Reputation: 8331
I've got this repetitive query:
Model.where(:foo1 => "bar", :foo2 => "bar", :foo3 => "bar", ..., :foo9 => "bar")
I wondered if there was a nice way to shorten the code, similar to the opposite case:
Model.where(:foo => ["bar1, bar2, bar3, ..., bar9"])
Is this possible?
Upvotes: 0
Views: 33
Reputation: 3438
Another option besides sql suggestion would be to use inject
method:
query = [:foo1, :foo2, :foo3].inject({}){|hash, key| hash[key] = "bar"; hash}
# => {:foo1=>"bar", :foo2=>"bar", :foo3=>"bar"}
Model.where(query)
Upvotes: 0