views:

19

answers:

1

Short version: Where should I store environment-specific IDs? ENV['some-variable']? Somewhere else?

Long version:

Let's say I have a model called Books and a book has a Category. (For the sake of this question, let's say a book only has one category.)

class Book < ActiveRecord::Base
  belongs_to :category
end

class Category < ActiveRecord::Base
  has_many :books
end

Now let's say one category is called 'erotica.' And I want to suppress erotica books in my type ahead. That seems straight forward. But in production and in development 'erotica' has a different ID. I don't want my code to be ID dependent. I don't want it to be string dependent (in case 'erotica' is renamed pr0n or whatever).

I think I should have something like

def suppress_method
  suppress_category_id = look_up_suppression_id
  ...
end

but where should 'look up' look?

Thanks!

A: 

Taking this approach will be brittle, what if you want to suppress multiple categories? Erotica and Politics? The best design here is for you to actually add 'suppressed' as a boolean to category in a migration, and maintain that in your application's administration interface. After you've done that you can add a named scope like:

class Category < ActiveRecord::Base
  named_scope :not_suppressed, :conditions=>{:suppressed=>false}
  # or for rails 3
  scope :not_suppressed, where(:suppressed=>false)
end

Then just update your type ahead code to do:

Category.not_suppressed.find ...

Rather than

Category.find
NZKoz
I agree that would cleaner. However, I am really just more interested in where a environment specific value like that should be stored. And what's the best way to store/recover it?
jmauster