Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 型号->;有很多->;两次_Ruby On Rails_Model_Associations_Has Many_Has Many Through - Fatal编程技术网

Ruby on rails 型号->;有很多->;两次

Ruby on rails 型号->;有很多->;两次,ruby-on-rails,model,associations,has-many,has-many-through,Ruby On Rails,Model,Associations,Has Many,Has Many Through,所以我这里有一个有点混乱的关系,在一个注释、组和用户之间。在我的模型中,我有过两次这样的经历。但我现在关注的是Note&Group的关系 背景:一个小组可以有一张便条。用户也可以有注释。这就是为什么我的笔记是多态的。但是,我还创建了一个称为标记的连接模型,以便一个注释可以属于多个组。但在我的代码中,我得到了多个“has_many:notes”。请参阅下面我的所有代码。这样做的正确方式是什么 提前谢谢 附注.rb user.rb group.rb tag.rb 你只需要给它一个不同的名字 clas

所以我这里有一个有点混乱的关系,在一个注释、组和用户之间。在我的模型中,我有过两次这样的经历。但我现在关注的是Note&Group的关系

背景:一个小组可以有一张便条。用户也可以有注释。这就是为什么我的笔记是多态的。但是,我还创建了一个称为标记的连接模型,以便一个注释可以属于多个组。但在我的代码中,我得到了多个“has_many:notes”。请参阅下面我的所有代码。这样做的正确方式是什么

提前谢谢

附注.rb user.rb group.rb tag.rb
你只需要给它一个不同的名字

class Group
  has_many :notes, :as => :notable
  has_many :tags
  has_many :tagged_notes, :class_name => 'Note', :through => :tags
end
如果您只想为
:as=>:notified
部分添加一个注释(这在您的问题中不是很清楚),您可以这样做:

class Group
  has_one :note, :as => :notable
  has_many :tags
  has_many :notes, :through => :tags
end

名字必须不同。虽然使用
note
notes
相比,代码其他部分的区别可能不是很清楚。

好吧,一个团队总是有很多注释,而不仅仅是一个。我想做的就是这样。有人可以写一个便笺,然后选中他们想要将便笺应用到哪些组的一些框。所以一个组可以有很多音符,一个音符可以属于许多不同的组。这更有意义吗?谢谢你的回答,我想我会把它称为一张带标签的便条,我对此不太熟悉。
has_many :notes, :as => :notable
has_many :tags
has_many :notes, :through => :tags
belongs_to :note
belongs_to :group
class Group
  has_many :notes, :as => :notable
  has_many :tags
  has_many :tagged_notes, :class_name => 'Note', :through => :tags
end
class Group
  has_one :note, :as => :notable
  has_many :tags
  has_many :notes, :through => :tags
end