简体   繁体   English

Rails与蒙古语的多态关联

[英]Rails polymorphic association with mongoid

Using Rails 3.2 with Mongoid 3.1.5. 将Rails 3.2与Mongoid 3.1.5结合使用。 I recently just changed the Event belongs_to :venue into a polymorphic association :location. 最近,我刚刚将Event Emirates_to:venue更改为多态关联:location。 After doing this, when saving the event scoped from the Organization, it no longer associates the event to the venue. 执行此操作后,在保存组织范围内的事件时,它将不再将事件与场所关联。

#Models
class Event
  include Mongoid::Document
  has_and_belongs_to_many :organizations, index: true
  belongs_to :location, polymorphic: true, index: true
end

class Organization
  include Mongoid::Document
  has_and_belongs_to_many :events, index: true
end

class Venue
  include Mongoid::Document
  has_many :events, as: :location, autosave: true
end

#Code

org = Organization.first
ven = Venue.first
evt = org.events.create(location: ven)
org.events.count #=> 1
evt.location #=> #<Venue...

# How can I make this include the evt?
ven.events.count #=> 0

From this, I could just do ven.events << evt , but that would require me doing that every time. 由此,我可以执行ven.events << evt ,但这需要我每次都这样做。 Any other ideas out there? 还有其他想法吗?

There is no "join" on Mongodb, so it is impossible to eager load your relationship. Mongodb上没有“ join”,因此不可能急于加载您的关系。 You can denormalize your data and embed a copy document from Venue inside the Event. 您可以对数据进行非规范化处理,并将副本文档从活动场所嵌入事件内部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM