mercredi 15 juin 2016

Rails - trouble using has_many relation

I apologize if this is a big question. I used to be good at rails but it has been a long time.

I am working a rails project and am having trouble with the has_many relation.

I have the following tables:

User
SchoolClass
Question
UserClassQuestion

Inside the models i have:

user.rb
   has_many :questions, :through => :user_class_questions
   has_many :user_class_questions

school_class.rb
   has_many :questions, :through => :user_class_questions
   has_many :user_class_questions

question.rb
   belongs_to :schoolclass
   belongs_to :user

user_class_question.rb
   belongs_to :question

So, what I want is to on a users home page, display let them view the questions they have asked with current_user.questions. This works.

But on the SchoolClass show page, if i say @school_class.questions, i get the following error:

!!<ActiveRecord::StatementInvalid: SQLite3::SQLException: no such colum:
 user_class_questions.school_class_id: SELECT "questions".* FROM "questions" 
INNER JOIN "user_class_questions" ON "questions"."id" = 
"user_class_questions"."question_id" WHERE 
"user_class_questions"."school_class_id" = ?>

@school_class is an object set by the params

def set_school_class
   @school_class = SchoolClass.find(params[:id])
end

The columns in UserClassQuestion table are:

{ user_id: , schoolclass_id: , question_id }

So, do I have something set up wrong?

Aucun commentaire:

Enregistrer un commentaire