使用 NOT 运算符的多关联连接查询
Multiple association joins query with NOT operator
我有以下 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 运算符放入多关联连接查询中?
您可以在 Rails 中尝试使用 .not()
4:
@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 查询,效果很好:-
@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 运算符放入多关联连接查询中?
您可以在 Rails 中尝试使用 .not()
4:
@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"})