我需要将其重构为Rails 4样式
型号名称 Invoice
我有
named_scope :with_name_like, lambda { |name|
name.blank? ? {} : { :conditions => ['LOWER(name) LIKE ?', "%#{name.downcase}%"], :joins => :client }
}
我做了
def self.with_name_like
->(name){where('LOWER(name) LIKE ?', "%#{name.downcase}%").joins(:client)}
end
但是得到
Invoice.with_name_like
=> #<Proc:0x00000004f35d50@/home/stasyan/current/app/models/invoice.rb:23 (lambda)>
如何将三元运算符用于此方法?用lambda?
以及如何使用最新语法
named_scope :ordered, :order => "date DESC"
谢谢
您可以简单地使用all
:
scope :with_name_like, ->(name) {
name.blank? ? all : where('LOWER(name) LIKE ?', "%#{name.downcase}%").joins(:client)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句