2017-08-05 90 views
-2

我有一個連接表; ThingLocation的事件和地點,想要添加另一個mudule:簡介用連接表導軌多個模塊

thing_location.rb

class ThingLocation < ActiveRecord::Base 
belongs_to :event 
belongs_to :location 
end 

event.rb

class Event < ApplicationRecord 
has_many :thing_locations 
has_many :locations, through: :thing_locations 
end 

location.rb

class Location < ApplicationRecord 
has_many :thing_locations 
has_many :events, through: :thing_locations 
end 

所有做工精細直到我想添加第三個模塊:配置文件通過連接表ThingL使用可用的位置ocation。

當我嘗試在配置文件更新搶位置的形式,它拋出錯誤「事件必須存在」

刪除:從thing_locations.rb「belongs_to的事件」解決,但是這將刪除之間的關聯型材和事件

....添加新的模型之後:配置文件

thing_location.rb

class ThingLocation < ActiveRecord::Base 
belongs_to :event 
belongs_to :location 
belongs_to :profile 

end 

event.rb

class Event < ApplicationRecord 

has_many :thing_locations 
has_many :locations, through: :thing_locations 

belongs_to:profile 
end 

location.rb

class Location < ApplicationRecord 
has_many :thing_locations 
has_many :events, through: :thing_locations 
has_many :profiles, through: :thing_locations 
end 

profile.rb

class Profile < ApplicationRecord 
has_many :thing_locations 
has_many :locations, through: :thing_locations 

has_many :hows, dependent: :destroy 

end 

回答

0

就在導軌5中,加入 「可選:真」 的連接模塊修復了問題:

thing_location.rb

class ThingLocation < ActiveRecord::Base 
belongs_to :event, optional: true 
belongs_to :location 
belongs_to :profile, optional: true