在 Ruby on Rails 中使用命名范围/lambda 进行空属性选择
我有一个 JobBreakdown
对象 has_one :invoice
。如果JobBreakdown 有发票(即invoice_id 不为nil),那么它被视为已开票。如果不是,则考虑UNINVOICED。用户希望能够从下拉框中选择“已开票”或“未开票”并显示正确的记录。
我将如何编写命名范围来测试并返回正确的记录?注意
named_scope :is_invoiced, lambda {|is_invoiced| {:conditions => :invoice.nil? == is_invoiced}}
:我使用的是 ruby 1.8.7,rails 2.3.5
I have an object JobBreakdown
that has_one :invoice
. If a JobBreakdown has an invoice (i.e. invoice_id is not nil) then it is considered INVOICED. If not, it is consider UNINVOICED. Users want to be able to select from a drop down box Invoiced or Uninvoiced and have correct records show up.
How would I write a named scope to test and return the right records? Something along the lines of
named_scope :is_invoiced, lambda {|is_invoiced| {:conditions => :invoice.nil? == is_invoiced}}
NB: I am using ruby 1.8.7, rails 2.3.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定,这是 Rails 2.x 的正确语法,但至少它会让您了解如何做到这一点。
或者甚至可能是这样的:
I'm not sure, this is the right syntax for rails 2.x but at least it'll give you an idea of how to do that.
or maybe even something like this: