Jaqx
Jaqx

Reputation: 826

What is the syntax for a ruby inequality in where query

What is the proper syntax to query where owner is not current_user:

.where(owner_id: != current_user.id, recipient_id: current_user.id, owner_type: "User")

I tried this:

where(['current_user.id <> ?', :owner_id ], recipient_id: current_user.id, owner_type: "User")

And Got:

undefined method `%' for ["current_user.id <> ?", :owner_id]:Array

Upvotes: 0

Views: 303

Answers (2)

house9
house9

Reputation: 20614

try using a separate where method call for each condition, these will generate sql AND

Something.where("owner_id <> ?", current_user.id)
  .where("recipient_id = ?", current_user.id)
  .where("owner_type = ?", "User")

Upvotes: 1

Huy
Huy

Reputation: 11206

I posted a solution in the comment, but you can also do something like

.where("owner_id != #{current_user.id} AND recipient_id = #{current_user.id} AND owner_type = \"User\"")

Upvotes: 1

Related Questions