将 Rails 3.2 与 Mongoid 3.1.5 一起使用。我最近刚刚将 Event belongs_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
,但这需要我每次都这样做。还有其他想法吗?