2017-08-05 57 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