【发布时间】:2011-09-01 03:18:54
【问题描述】:
所以我在这里有一个有点令人困惑的关系,即注释、组和用户之间的关系。最后我在我的模型中使用了 has_many 两次。但我目前专注于 Note 与 Group 的关系。
背景:一个组可以有一个注释。用户也可以有注释。这就是为什么我的笔记是多态的。但是,我还创建了一个名为 Tag 的连接模型,以便一个 Note 可以属于多个组。不过,在我的代码中,我最终得到了多个“has_many :notes”。请参阅下面的所有代码。做这样的事情的正确方法是什么?
提前致谢!
note.rb
belongs_to :notable, :polymorphic => true
has_many :tags
has_many :groups, :through => :tags
user.rb
has_many :notes, :as => :notable
group.rb
has_many :notes, :as => :notable
has_many :tags
has_many :notes, :through => :tags
tag.rb
belongs_to :note
belongs_to :group
【问题讨论】:
标签: ruby-on-rails model associations has-many has-many-through