2011-11-03 61 views
0

如果一个用户有很多东西,而某个东西有很多统计信息,看起来只有Rails-y的方式来通过用户公开统计信息。通过父项暴露has_many - > has_many的关联?

class User < ActiveRecord::Base 
    has_many :things do 
    def stats 
     Stat.where(thing_id: proxy_association.owner.things_id) 
    end 
    end 
end 

class Thing < ActiveRecord::Base 
    belongs_to :user 
    has_many :stats 
end 

class Stat < ActiveRecord::Base 
    belongs_to :thing 
    has_one :user, through: :thing 
end 

User.first.things.stats == Stat.where(thing_id: User.first.thing_ids) 

我试图确定是否有任何其他选项。有人在我的球队抱怨,这不自然的感觉。我觉得这是你可以设计的关系中最自然的表达。

有没有人有更好的建议?我会说,我尝试过实例方法,但他们闻不到对。

回答