You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The User model has a feed method that is currently implemented the following way:
# Listing 14.47: The final implementation of the feeddeffeedfollowing_ids="SELECT followed_id FROM relationships WHERE follower_id = :user_id"Micropost.where("user_id IN (#{following_ids}) OR user_id = :user_id",user_id: id)end# Listing 14.50: Using a join to make the feed.deffeedpart_of_feed="relationships.follower_id = :id or microposts.user_id = :id"Micropost.joins(user: :followers).where(part_of_feed,{id: id})end
I think a slightly more elegant solution may be proposed here, for instance:
The
User
model has afeed
method that is currently implemented the following way:I think a slightly more elegant solution may be proposed here, for instance:
The following SQL query is produced by the code above:
As you can see, just like the solution currently used, the one proposed here also produces a single SQL query with subquery, but:
default_scope
that has been used earlierOR
with ActiveRecordThe text was updated successfully, but these errors were encountered: