繁体   English   中英

Rails与蒙古语的多态关联

[英]Rails polymorphic association with mongoid

将Rails 3.2与Mongoid 3.1.5结合使用。 最近,我刚刚将Event Emirates_to:venue更改为多态关联:location。 执行此操作后,在保存组织范围内的事件时,它将不再将事件与场所关联。

#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

由此,我可以执行ven.events << evt ,但这需要我每次都这样做。 还有其他想法吗?

Mongodb上没有“ join”,因此不可能急于加载您的关系。 您可以对数据进行非规范化处理,并将副本文档从活动场所嵌入事件内部。

暂无
暂无

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

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