Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not find the association problem in Rails

I am fairly new to Ruby on Rails, and I clearly have an active record association problem, but I can't solve it on my own.

Given the three model classes with their 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 

But when I execute the controller to add questions to application forms, I get the error:

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 

Can anyone point out what I am doing wrong?

like image 336
Ash Avatar asked Nov 23 '09 05:11

Ash


People also ask

What are associations in Rails?

Association in Rails defines the relationship between models. It is also the connection between two Active Record models. To figure out the relationship between models, we have to determine the types of relationship. Whether it; belongs_to, has_many, has_one, has_one:through, has_and_belongs_to_many.


2 Answers

In the ApplicationForm class, you need to specify ApplicationForms's relationship to 'form_questions'. It doesn't know about it yet. Anywhere you use the :through, you need to tell it where to find that record first. Same problem with your other classes.

So

# 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 

That is assuming that's how you have it set up.

like image 155
Jim Avatar answered Sep 28 '22 06:09

Jim


You need to include a

has_many :form_question_answers 

In your FormQuestion model. The :through expects a table that's already been declared in the model.

Same goes for your other models -- you can't supply a has_many :through association until you've first declared the 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 

It looks like your schema might be a bit wonky, but the point is you always need to add the has_many for the join table first, then add the through.

like image 44
bensie Avatar answered Sep 28 '22 07:09

bensie