0

I'm trying to set up two environments for my site, one on a development server, one on a live one. And I want to dictate which records from the DB can be seen on each server. I've created a file in my includes, for development it has this @show = "dev" and for live it has @show = "live" I've included this at the top of my application layout so it is on every page. Then in my views, with each database call I want to put some conditions like this:

- f = Event.find(:all, :conditions => ["#{@show} = 1"])

But it doesn't pick up @show as being the variable, just uses it explicitly or ignores it. Is there a simple way to do this or will this not work how I expect it to?

update

I've managed to make it work, but I have to include the file on each individual view rather than just on the application layout... Not ideal but it's a solution:

= render "includes/dev_live"

- f = Event.find(:all, :conditions => {@show => 1})
5
  • what is preventing you from having separate databases for each environment? Commented Jul 15, 2013 at 8:58
  • I don't want to have two copies of everything, that's how it was before and everything was out of sync Commented Jul 15, 2013 at 8:59
  • Using text substitution inside a query is kinda frowned upon, due to the possibility of SQL injection. I suspect it is unlikely that the value of @show can be changed by a malicious user, it is good practice to use the hash form for conditions (as in the two answers below) Commented Jul 15, 2013 at 9:26
  • 1
    I would suggest using the built-in rails environment accessor (Rails.env) rather than your own global variable. Commented Jul 15, 2013 at 9:27
  • Why would you need up-to-date data on development? And what is stopping you from dumping production db everyday into development? Commented Jul 15, 2013 at 14:11

2 Answers 2

1

I think you would be better off changing your variables to symbols i.e

@show = :dev
@show = :live

then your active record query could become:

f = Event.find(:all, :conditions => {@show => 1})
Sign up to request clarification or add additional context in comments.

Comments

0

you need to convert a Variable into instance variable. for using it in conditions.

You can try this

f = Event.find(:all, :conditions => {@show.to_sym => 1 })

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.