Mongoid::错误::混合关系:Mongoid +设计

发布于 2024-12-10 09:39:38 字数 783 浏览 0 评论 0原文

我有一个来自 Devise 的模型用户,具有这种关系:

user.rb

# Relationships  
references_many :houses, :dependent => :delete

现在我有一个用脚手架创建的模型屋:

house.rb

# Relationships
referenced_in :user, :inverse_of => :houses 
embeds_many :deals

现在我有一个模型处理这种关系:

embedded_in :house, :inverse_of => :deals

在我的 paths.rb 中,我有:

resources :houses do
  resources :deals
end

当我尝试获取在控制台中进行交易的用户:

ruby-1.9.2-p180 :009 > User.first.deals.first

我收到下一个错误:

Mongoid::Errors::MixedRelations: Referencing a(n) Deal document from the User document via a relational association is not allowed since the Deal is embedded.

I have a Model User from Devise with that relations:

user.rb

# Relationships  
references_many :houses, :dependent => :delete

Now I have a Model House created with scaffold:

house.rb

# Relationships
referenced_in :user, :inverse_of => :houses 
embeds_many :deals

Now I have a Model Deal with this relations:

embedded_in :house, :inverse_of => :deals

In my routes.rb I have:

resources :houses do
  resources :deals
end

When I try get the user that make the deal in console:

ruby-1.9.2-p180 :009 > User.first.deals.first

I get the next error:

Mongoid::Errors::MixedRelations: Referencing a(n) Deal document from the User document via a relational association is not allowed since the Deal is embedded.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻的心爱 2024-12-17 09:39:38

根据您提供的信息,用户与交易没有直接关系。

您似乎试图这样做:

class User
  [...]
  references_many :houses, :dependent => :delete
  references_many :deals
end

class Deal
  [...]
  embedded_in :house
  referenced_in :user
end

由于您的交易嵌入到房屋中,因此您无法通过关系直接从用户访问它们。
这是 Mongoid 的一个已知限制。

您可以使用:

@houses_that_match = House.where("deals.user_id" => @user.id)
@deals = []
@houses_that_match.each do |house|
  @deals += house.deals.select { |deal| deal.user == @user }
end

With the information you provided, an user is not directly related to a deal.

It seems that you tried to do :

class User
  [...]
  references_many :houses, :dependent => :delete
  references_many :deals
end

class Deal
  [...]
  embedded_in :house
  referenced_in :user
end

As your Deals are embedded into Houses, you can't access them directly from Users through a relation.
It is a known limitation of Mongoid.

You can use :

@houses_that_match = House.where("deals.user_id" => @user.id)
@deals = []
@houses_that_match.each do |house|
  @deals += house.deals.select { |deal| deal.user == @user }
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文