在 Ruby on Rails 中使用命名范围/lambda 进行空属性选择

发布于 2024-11-18 06:38:46 字数 377 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如若梦似彩虹 2024-11-25 06:38:46

我不确定,这是 Rails 2.x 的正确语法,但至少它会让您了解如何做到这一点。

named_scope :is_invoiced, lambda { |is_invoiced| :condition => is_invoiced ? "invoice IS NOT NULL" : "invoice IS NULL" }

或者甚至可能是这样的:

named_scope :is_invoiced, lambda { |is_invoiced| :condition => "invoice IS #{is_invoiced ? 'NOT' : ''} NULL" }

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.

named_scope :is_invoiced, lambda { |is_invoiced| :condition => is_invoiced ? "invoice IS NOT NULL" : "invoice IS NULL" }

or maybe even something like this:

named_scope :is_invoiced, lambda { |is_invoiced| :condition => "invoice IS #{is_invoiced ? 'NOT' : ''} NULL" }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文