【问题标题】:Multiple association joins query with NOT operator使用 NOT 运算符的多个关联连接查询
【发布时间】:2015-05-02 13:24:43
【问题描述】:

我有以下 Rails 查询,效果很好:-

@basic = Pay.joins(:ee_pay => :company_pay).where(:pays => {pay_line_id: current_pay_line.id, pay_sub_head_id: 1}, :company_pays => {:description => 'Salary'}).first

我现在正在尝试编写 NOT 等效项,但失败得很惨。我认为这会起作用,但它一直给我错误:-

@other_pay = Pay.joins(:ee_pay => :company_pay).where(:pays => {pay_line_id: current_pay_line.id, pay_sub_head_id: 1}, :company_pays => {('description != ?', "Salary")})

还尝试了这个和其他一些变体:-

@other_pay = Pay.joins(:ee_pay => :company_pay).where(:pays => {pay_line_id: current_pay_line.id, pay_sub_head_id: 1}).where(:company_pays => ['description != ?', "Salary"])

谁能告诉我如何在多重关联联接查询中放置 NOT 运算符?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 activerecord rails-activerecord ruby-on-rails-4.1


    【解决方案1】:

    您可以在 Rails 4 中尝试使用 .not()

    @other_pay = Pay.joins(ee_pay: :company_pay)
                    .where(pays: {pay_line_id: current_pay_line.id, pay_sub_head_id: 1})
                    .where.not(company_pays: {description: "Salary"})
    

    【讨论】:

    • Rails 版本为 4.1.0
    猜你喜欢
    • 2013-06-07
    • 1970-01-01
    • 2014-01-21
    • 2020-12-18
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    相关资源
    最近更新 更多