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

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -