Array conditions placeholder substitution problem (Rails3 Active Record)
Could anyone please tell why this code won't work:
scope :scope开发者_StackOverflow社区_a, lambda {|x, y| select('DISTINCT ?', x).order('? ASC', y)}
(says ArgumentError: wrong number of arguments (2 for 1)), whereas this
scope :scope_b, lambda {|x, y| where(:cond1 => x).where(:cond2 => y)}
works perfectly?
How does one construct a proper scope with conditions like the 1st example (when seemingly it's impossible to use hash conditions)?
UPD The safe solution turned out to be trivial:
scope :myscope, lambda{|field, mode| {:select => field, :conditions => {:moderated => mode}, :order => field}}
..but doesn't this way of setting scope conditions get deprecated (not sure) in future RoR versions?
The approach you're taking only works for the where(...)
method, not for select(...)
(which is what's throwing the error) or for order(...)
.
You'll probably want to do this (since an order
clause will default to be ascending, you can just pass in y
directly):
scope :scope_a, lambda {|x, y| select("DISTINCT #{x}").order(y)}
精彩评论