2009-11-23 10 views
31

Je suis relativement nouveau dans Ruby on Rails, et j'ai clairement un problème d'association d'enregistrements actif, mais je ne peux pas le résoudre seul.Impossible de trouver le problème d'association dans Rails

Compte tenu des trois classes de modèles avec leurs associations:

# application_form.rb 
class ApplicationForm < ActiveRecord::Base 
    has_many :questions, :through => :form_questions 
end 

# question.rb 
class Question < ActiveRecord::Base 
    belongs_to :section 
    has_many :application_forms, :through => :form_questions 
end 

# form_question.rb 
class FormQuestion < ActiveRecord::Base 
    belongs_to :question 
    belongs_to :application_form 
    belongs_to :question_type 
    has_many :answers, :through => :form_question_answers 
end 

Mais quand j'exécute le contrôleur pour ajouter des questions aux formulaires de demande, je reçois l'erreur:

ActiveRecord::HasManyThroughAssociationNotFoundError in Application_forms#show 

Showing app/views/application_forms/show.html.erb where line #9 raised: 

Could not find the association :form_questions in model ApplicationForm 

Quelqu'un peut-il indiquer ce Je fais mal?

Répondre

57

Dans la classe ApplicationForm, vous devez spécifier la relation ApplicationForms à 'form_questions'. Il ne le sait pas encore. Partout où vous utilisez le :through, vous devez indiquer où trouver cet enregistrement en premier. Même problème avec vos autres classes.

Alors

# application_form.rb 
class ApplicationForm < ActiveRecord::Base 
    has_many :form_questions 
    has_many :questions, :through => :form_questions 
end 

# question.rb 
class Question < ActiveRecord::Base 
    belongs_to :section 
    has_many :form_questions 
    has_many :application_forms, :through => :form_questions 
end 

# form_question.rb 
class FormQuestion < ActiveRecord::Base 
    belongs_to :question 
    belongs_to :application_form 
    belongs_to :question_type 
    has_many :form_questions_answers 
    has_many :answers, :through => :form_question_answers 
end 

Cela suppose que comment vous l'avez mis en place.

+1

je l'ai fait un millier de fois et même en regardant mes autres modèles hmt de travail, je ne pouvais pas voir que je manquais l'autre has_many ... lol ... – Danny

11

Vous devez inclure un

has_many :form_question_answers 

Dans votre modèle FormQuestion. Le: through attend une table qui a déjà été déclarée dans le modèle.

va de même pour vos autres modèles - vous ne pouvez pas fournir une association has_many :through jusqu'à ce que vous avez d'abord déclaré le has_many

# application_form.rb 
class ApplicationForm < ActiveRecord::Base 
    has_many :form_questions 
    has_many :questions, :through => :form_questions 
end 

# question.rb 
class Question < ActiveRecord::Base 
    belongs_to :section 
    has_many :form_questions 
    has_many :application_forms, :through => :form_questions 
end 

# form_question.rb 
class FormQuestion < ActiveRecord::Base 
    belongs_to :question 
    belongs_to :application_form 
    belongs_to :question_type 
    has_many :form_question_answers 
    has_many :answers, :through => :form_question_answers 
end 

Il semble que votre schéma pourrait être un peu bancal, mais le point est vous devez toujours ajouter le has_many pour la table de jointure, puis ajouter le travers.

+0

Merci beaucoup pour cela - vous êtes bien sûr, sur place avec votre réponse. Cependant, "il ne peut y en avoir qu'un", j'ai donc accordé la réponse à l'autre parce qu'il a répondu en premier. J'ai voté pour votre réponse, donc il y avait au moins un prix de consolation à avoir la bonne réponse. Merci une tonne. :) – Ash