我需要将它重构为rails 4 style
型号名称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"
由于
答案 0 :(得分:1)
您只需使用all
:
scope :with_name_like, ->(name) {
name.blank? ? all : where('LOWER(name) LIKE ?', "%#{name.downcase}%").joins(:client)
}
答案 1 :(得分:0)
我建议
def self.with_name_like(name)
if name
where('LOWER(name) LIKE ?', "%#{name.downcase}%").joins(:client)
else
self
end
end
我比参数化范围定义更喜欢类方法。