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?
The error you're experiencing is most likely due to the variation in your column name. In Rails, school_class_id
is not the same as schoolclass_id
. I think a quick way to work around, is to specify the primary_key/foreign_key
in your belongs_to option.
A more lasting solution is to run a migration to rename your column to school_class_id
rather than schoolclass_id
.
Let me know if I was able to help.
UPDATE
if the belongs_to :school_class
is defined on question
, then you have access to question.school_class and your school_class model should just have has_many :questions, no need of running to a join table to do that as that would confuse rails a bit. If however you don't want it this way you could comment out the belongs_to :school_class
assoication and instead have a has_many :user_class_questions
on question
class User
has_many :questions, through: :user_class_questions
has_many :user_class_questions
end
class SchoolClass
has_many :questions, through: :user_class_questions
has_many :user_class_questions
end
class Question
#belongs_to :school_class
belongs_to :user
has_many :user_class_questions
end
class UserClassQuestion
belongs_to :question
belongs_to :school_class
belongs_to :user
end
Your Data model needs a bit of a change, your UserClassQuestion
is a join model and it should have data about the User, SchoolClass, and Question. So the relationship should be something like
UserClassQuestion
rails g model UserClassQuestion user:references school_class:references question:references
class UserClassQuestion < ActiveRecord::Base
belongs_to :user
belongs_to :school_class
belongs_to :question
end
school_class.rb
class SchoolClass < ActiveRecord::Base
has_many :user_class_questions
has_many :questions, through: :user_class_questions
end
user.rb
class User < ActiveRecord::Base
has_many :user_class_questions
has_many :questions, through: :user_class_questions
end
And then you can do user.questions
and school_class.questions
Also try not to use has_many_and_belongs_to association, I feel it is a bad idea, it creates a table without a primary id, and later on if you want to do something else with this join table, then this will come and bite you. has_many, through is a bit extra step but it is worth it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.