2014-04-13 62 views
0

我试图创建一个多态多对多的关系this教程,但遇到了问题。当我尝试访问@ friends.post,我得到这个:创建一个多态多对多的关系

SystemStackError: stack level too deep 
    from /Users/mattmoss/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/irb/workspace.rb:86 
Maybe IRB bug! 

这里是我使用创建我的关系代码:

在帖子:

has_many :friends, :through => :taggings, :source => :taggable, :source_type => "Friend" 
has_many :taggings 

在职位:

belongs_to :post 
belongs_to :postable, :polymorphic => true 

在好友:

has_many :posts, :through => :postings 
has_many :postings, :through => :posttable, :source_type => "Friend" 

当我打电话给friends.posts时,出现上面列出的错误。

+0

你确定职位有很多朋友意义? –

+0

嘿,我刚才也有这个相同的错误,那是因为我没有正确设置关联。我认为你需要重新考虑你的关联。请向我们介绍您希望如何关联您的关联,我们会尽力帮助 – Peege151

+0

因此,它将像标签一样设置,因此一个帖子可以包含多个标签,但标签也可以包含很多帖子,评论等等。我正在寻找一个设置就像这篇文章中的一个@ Peege151 http://blog.dharanasoft.com/2011/09/06/polymorphic-many-to-many-associations-in-rails/ – matthew

回答

0

您收到此错误,因为当你的朋友模型具有自我引用关系

删除:SOURCE_TYPE =>“朋友”从朋友的has_many:帖子

has_many :posts, :through => :postings 
has_many :postings, :through => :posttable