0

背景:我有一个简单的has_many:通过链接社区和帖子的设置 - 它允许我打电话@ community.posts或@ post.communities。我还有一个事件模型,它继承自文章has_many:通过继承类的方法

目标:我想知道是否有Rails的方式来调用@ community.events的行来只返回属于该社区的事件。

+0

你尝试过什么?我会尝试在事件中声明has_many关系。 – bento 2012-07-26 12:38:19

+0

我试过了,尽管事件从post类继承,不应该只是在post.rb中声明它们一次就足够了吗? – neon 2012-07-26 13:05:24

回答

0

使用这些协会在社区模式,

class Community < ActiveRecord::Base 
    has_and_belongs_to_many :posts, conditions: {type: nil} 
    has_and_belongs_to_many :events, join_table: "communities_posts", association_foreign_key: "post_id" 
end 

对于过后,你可以简单地使用,

has_and_belongs_to_many :communities 
+0

不幸的是,我需要的关系是has_many:通过...你将如何做到这一点? – neon 2012-07-27 05:07:58