Ruby on rails rubyonrails&;mongoid-关系管理属于,有很多
我对mongoid/rails关系有一个问题,我知道有很多关于此类问题的主题,但我找不到任何对我有帮助的。。 我有这些模型:Ruby on rails rubyonrails&;mongoid-关系管理属于,有很多,ruby-on-rails,mongoid,Ruby On Rails,Mongoid,我对mongoid/rails关系有一个问题,我知道有很多关于此类问题的主题,但我找不到任何对我有帮助的。。 我有这些模型: class Project include Mongoid::Document belongs_to :owner, :class_name => 'User', inverse_of: :projects has_many :members end class Member include Mongoid::Document belong
class Project
include Mongoid::Document
belongs_to :owner, :class_name => 'User', inverse_of: :projects
has_many :members
end
class Member
include Mongoid::Document
belongs_to :project, inverse_of: :members
belongs_to :user
end
class User
include Mongoid::Document
has_many :projects, inverse_of: :user
end
当我尝试将用户记录为成员时,出现以下错误:
Mongoid::Errors::InverseNotFound (
message:
When adding a(n) User to Project#members, Mongoid could not determine the inverse foreign key to set. The attempted key was 'project_id'.
summary:
When adding a document to a relation, Mongoid attempts to link the newly added document to the base of the relation in memory, as well as set the foreign key to link them on the database side. In this case Mongoid could not determine what the inverse foreign key was.
resolution:
If an inverse is not required, like a belongs_to or has_and_belongs_to_many, ensure that :inverse_of => nil is set on the relation. If the inverse is needed, most likely the inverse cannot be figured out from the names of the relations and you will need to explicitly tell Mongoid on the relation what the inverse is.
Example:
class Lush
include Mongoid::Document
has_one :whiskey, class_name: "Drink", inverse_of: :alcoholic
end
class Drink
include Mongoid::Document
belongs_to :alcoholic, class_name: "Lush", inverse_of: :whiskey
end):
我不明白为什么,我认为这两种关系有问题,相反的关系,但我不知道如何解决这个问题
class Project
include Mongoid::Document
belongs_to :owner, :class_name => 'User', inverse_of: :projects
has_many :members
has_many :users
end
但我不认为你的建模真的能达到你想要的。在关系数据库中,可以使用与联接表的间接关联:
class User < ActiveRecord::Base
has_many :memberships
has_many :projects, through: :memberships
end
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :project
end
class Project < ActiveRecord::Base
has_many :memberships
has_many :members, through: :memberships,
source: :user
end
或者,如果需要能够向中间模型添加数据,则可以伪造间接关联:
class Project
# ...
embeds_many :memberships
def members
Patient.in(id: memberships.pluck(:user_id))
end
end
class Membership
# ...
field :approved, type: Boolean
belongs_to :user
embedded_in :project
end
class User
# ...
def projects
Project.where("members.user_id" => id).all
end
end
谢谢你的回答!我认为嵌入是一个好主意,但我如何为用户(所有者)和用户(仅为成员)管理它?我不完全确定,但我认为您可以使用类选项
embeddes\u many:members,class\u name:'user'
,embeds\u one:owner,class\u name:'user'
。但我已经有一段时间没有使用Mongoid了。我不确定最终嵌入它是否是一个好主意,因为我不希望我的用户依赖于project,我希望project和用户之间有一个关系查看我的编辑如何绕过限制嵌入“加入”文档。我在这台机器上没有mongodb,所以我还没有测试它。
class Project
# ...
embeds_many :memberships
def members
Patient.in(id: memberships.pluck(:user_id))
end
end
class Membership
# ...
field :approved, type: Boolean
belongs_to :user
embedded_in :project
end
class User
# ...
def projects
Project.where("members.user_id" => id).all
end
end