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)”, current_user.id, current_user.id) %>
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)”, current_user.id, current_user.id) %>
You can achieve this with:
<%
@friended = current_user.profile.friendables.where(“from_id = :user_id OR to_id = :user_id”, user_id: current_user.id).where(accepted: 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(current_user.id).or(Friendable.arel_table[:to_id].eq(current_user.id))).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:
current_user.profile.friendables.accepted.where(Friendable.arel_table[:from_id].eq(current_user.id).or(Friendable.arel_table[:to_id].eq(current_user.id)))
``
Jim