Ruby on Rails
Tuesday, April 12, 2016
I like to use a dash of Arel for this (took a guess at the friendables association class name, adjust as necessary):
I'd also consider adding an accepted scope to friendables:
Then, you could do this:
Jim
On Friday, April 8, 2016 at 4:45:42 AM UTC-4, Giedrius Rimkus wrote:
-- 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 }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
On Friday, April 8, 2016 at 4:45:42 AM UTC-4, Giedrius Rimkus wrote:
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) %>
On Thursday, April 7, 2016 at 6:15:53 PM UTC+3, fugee ohu wrote: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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/205c2eec-bb12-4472-928b-c2b66028334e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment