Reputation: 13467
I have the @user variable, and i have resources table, and then I have a favorites table which is merely user_id and resource_id
@user.resources.each
works obviously
@user.favorites.first.resource
works fine, except i want all of the resources.
@user.favorites.resources
does not work
resource.rb
belongs_to :category
belongs_to :user
has_many :favorites
has_many :resource_tags
has_many :tags, :through => :resource_tags
user.rb
has_many :resources
has_many :favorites
favorite.rb
belongs_to :resource
belongs_to :user
Upvotes: 0
Views: 62
Reputation: 5148
Try:
@user.favorites.includes(:resource).collect(&:resource)
That should eager load the resource from all the User's favorites and then the collect should return them as an Array.
Upvotes: 2