I am trying to get all of a user's friends' posts and user info in one query.
I have tried to use includes(:users) but that doesn't seem to work and throws an error. I figured a manual join would be the best solution:
@friends = @user.friends
.joins('inner join users on users.id = friends.user_id left outer join posts on users.id = posts.user_id')
.select('posts.*, users.*, friends.*')
How do you access attributes from users and posts? doing a @friends.inspect only shows attributes from the friends table, and I can't do @friends.posts or @friends.users.
My model for friends and users looks like this:
class Friend < ActiveRecord::Base
belongs_to :users
end
class User < ActiveRecord::Base
has_many :friends
has_many :users, through: :friends
end