2017-08-05 5 views
-2

J'ai une table Join; ThingLocation pour l'événement et l'emplacement et veut ajouter un autre mudule: ProfilRails plusieurs modules avec une table de jointure

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 

Tout beau travail jusqu'à ce que je voulais ajouter un troisième module: Profil pour utiliser les emplacements disponibles à travers la table de jointure ThingL ocation.

Lorsque je tente de saisir l'emplacement dans la mise à jour du profil forme qu'il jette l'erreur « événement doit exister »

Retrait « belongs_to: événement » du thing_locations.rb résout le mais cela supprimera l'association entre profils et événements

.... Après avoir ajouté un nouveau modèle: profils

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 

Répondre

0

Je viens de trouver dans des rails 5, en ajoutant "en option: true" dans le module rejoindre Corrige le problème:

thing_location.rb

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

fin