簡體   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