0

I have an array of symbols, and I'd like to declare a scope in an ActiveRecord model for each one.

The following does not work:

@@animal_types = [:horse, :cat, :dog]
@@animal_types.each do |a|
    scope a, :conditions => "animal_type = '#{a.to_s}'"
end

What would be the best way to achieve this?

Update: I was testing this incorrectly, this does in fact work

tereško
  • 58,060
  • 25
  • 98
  • 150
  • That seems to work for me in Rails 3.2. What version are you on? – rossta Feb 19 '13 at 22:48
  • this is on rails 3.0.20 – William Budington Feb 19 '13 at 22:48
  • Works for me in rail 3.0.20 as well. Do you get an error or any output on console, like when you run 'YourModel.horse.to_sql`? – rossta Feb 19 '13 at 22:57
  • sorry, I was testing this incorrectly - it does work for me. thanks for your help – William Budington Feb 19 '13 at 23:01
  • 1
    Just a comment: Avoid using @@variables, you can use @foo as well in context of a class. Assume the following: `class Foo;@@bar = :a;end` and `class Bar < Foo;@@bar = :b;end;` What's the value of `@@bar` within of Foo? Surprisingly, it is `:b`, `@@variables` are shared across "Subclasses" – Deradon Feb 19 '13 at 23:06

0 Answers0