Mongoid 3.1.5 で Rails 3.2 を使用する。最近、イベントの 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
、毎回それを行う必要があります. そこに他のアイデアはありますか?