【发布时间】:2011-04-01 22:45:36
【问题描述】:
为另一个模型的子集查找多个唯一关联模型的推荐方法是什么?例如,对于一部分用户,确定他们喜欢的独特艺术家模型。
一种方法是从数据库中获取用户,然后遍历所有用户以查找收藏夹并构建唯一数组,但这似乎效率低下且速度慢。
class User < ActiveRecord::Base
has_many :favorites
end
class Artist < ActiveRecord::Base
has_many :favorites
end
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :artist
end
@users = User.find_by_age(26)
# then determine unique favorited artists for this subset of users.
【问题讨论】:
标签: ruby-on-rails ruby activerecord associations