Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.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 Mongoid 1..N多态引用关系_Ruby_Mongodb_Mongoid - Fatal编程技术网

Ruby Mongoid 1..N多态引用关系

Ruby Mongoid 1..N多态引用关系,ruby,mongodb,mongoid,Ruby,Mongodb,Mongoid,我有以下型号 class Track include Mongoid::Document field :artist, type: String field :title, type: String has_many :subtitles, as: :subtitleset end class Subtitle include Mongoid::Document field :lines, type: Array belongs_to :subtitleset, p

我有以下型号

class Track
  include Mongoid::Document
  field :artist, type: String
  field :title, type: String
  has_many :subtitles, as: :subtitleset
end

class Subtitle
  include Mongoid::Document
  field :lines, type: Array
  belongs_to :subtitleset, polymorphic: true
end

class User
  include Mongoid::Document
  field :name, type: String
  has_many :subtitles, as: :subtitleset
end
在我的ruby代码中,当我创建一个新的字幕时,我将它推到适当的轨道上,用户如下所示:

Track.find(track_id).subtitles.push(subtitle)
User.find(user_id).subtitles.push(subtitle)
问题是它只在用户中被推送,而不在轨道中也被推送。但是如果我移除第二条线,它就会被推到轨道上。那为什么不同时为这两种人工作呢

我在副标题文档中看到:

"subtitleset_id" : ObjectId( "4e161ba589322812da000002" ),
"subtitleset_type" : "User"

如果字幕属于某个东西,它有一个指向该东西的ID。一个字幕不能同时属于两个人。如果归属是多态的,则字幕可以属于未指定类的内容,但它仍然不能同时属于两个内容

你想要:

class Track
  include Mongoid::Document
  field :artist, type: String
  field :title, type: String
  has_many :subtitles
end

class Subtitle
  include Mongoid::Document
  field :lines, type: Array
  belongs_to :track
  belongs_to :user
end

class User
  include Mongoid::Document
  field :name, type: String
  has_many :subtitles
end
然后您将能够:

Track.find(track_id).subtitles.push(subtitle)
User.find(user_id).subtitles.push(subtitle)

我遵循了这里指定的多态行为:(向下滚动到页面底部)。我也会试试你的建议。