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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.