sql - ActiveRecord Joins -
i'm trying use activerecord best of it's ability , having trouble doing joins in way like.
here how models set up:
class model has_many :likes end class belongs_to :model belongs_to :user end class user has_many :likes end
i likes 'model' object , there users made like. activerecord query is
@model = model.find(params[:id]) @users = @model.likes.joins(:user)
however, last line of code returns like, not user. there way use joins appropriately , want?
thanks,
you can has_many :through
:
class model has_many :likes has_many :users, :through => :likes end
or can eager load users , join in ruby:
@users = @model.likes.includes(:user).map(&:user)
Comments
Post a Comment