Can i use OR and also AND in the same where clause?

I need to select where column = (this||that) and

<% @friended = current_user.profile.friendables.where("(from_id = ? OR
to_id = ?) AND accepted = true)",, %>

You can achieve this with:

@friended = current_user.profile.friendables.where(“from_id = :user_id
to_id = :user_id”, user_id: true)

I like to use a dash of Arel for this (took a guess at the friendables
association class name, adjust as necessary):

where(accepted: true)

A bonus is that this will qualify the from_id/to_id columns with the
Friendable table name to avoid SQL errors if the column names clash with
any other tables included in the query.

I’d also consider adding an accepted scope to friendables:

scope :accepted, -> { where accepted: true }

Then, you could do this:



This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs