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

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

<% @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 OR 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):

current_user.profile.friendables.where(Friendable.arel_table[:from_id].eq([:to_id].eq( 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: