Noobie question about has_many :through


#1

First of all, I am a bit of a noob so sorry if this is a very simple
question.

Basically I want to model the relationship between a User (U) and a
Questionnaire(Qu). A Qu
has one Subject (the user that the questionnaire is about) and many
Reviewers (people/users that work
with the User answering the Qu about the U).

My migrations can be found here and models ca be
found

I tried the following in script/console

quest1 = Questionnaire.create(:title => "First One")

sub1 = User.create(:login => “sub1”, :email => “removed_email_address@domain.invalid”, :name
=> “Subject 1”, :password => “password”, :password_confirmation =>
“password”)

rev1 = User.create(:login => “rev1”, :email => “removed_email_address@domain.invalid”, :name
=> “reviewer 1”, :password => “password”, :password_confirmation =>
“password”)
rev2 = User.create(:login => “rev2”, :email => “removed_email_address@domain.invalid”, :name
=> “reviewer 2”, :password => “password”, :password_confirmation =>
“password”)

a_sub = Subjectships.create(:user_id => sub1.id, :questionnaire_id =>
quest1.id)
a_rev1 = Reviewerships.create(:user_id => rev1.id, :questionnaire_id
=> quest1.id)
a_rev2 = Reviewerships.create(:user_id => rev2.id, :questionnaire_id
=> quest1.id)

but when I tried quest1.subjects

I get the following error

NameError: uninitialized constant Questionnaire::Subjectship
from /Users/paulbrackenridge/Rails/projects/work/enabling
development/ED/vendor/rails/activerecord/lib/…/…/activesupport/lib/
active_support/dependencies.rb:477:in const_missing' from /Users/paulbrackenridge/Rails/projects/work/enabling development/ED/vendor/rails/activerecord/lib/active_record/base.rb: 1479:incompute_type’
from /Users/paulbrackenridge/Rails/projects/work/enabling
development/ED/vendor/rails/activerecord/lib/active_record/
reflection.rb:125:in send' from /Users/paulbrackenridge/Rails/projects/work/enabling development/ED/vendor/rails/activerecord/lib/active_record/ reflection.rb:125:inklass’
from /Users/paulbrackenridge/Rails/projects/work/enabling
development/ED/vendor/rails/activerecord/lib/active_record/
reflection.rb:169:in source_reflection' from /Users/paulbrackenridge/Rails/projects/work/enabling development/ED/vendor/rails/activerecord/lib/active_record/ reflection.rb:169:incollect’
from /Users/paulbrackenridge/Rails/projects/work/enabling
development/ED/vendor/rails/activerecord/lib/active_record/
reflection.rb:169:in source_reflection' from /Users/paulbrackenridge/Rails/projects/work/enabling development/ED/vendor/rails/activerecord/lib/active_record/ reflection.rb:178:incheck_validity!’
from /Users/paulbrackenridge/Rails/projects/work/enabling
development/ED/vendor/rails/activerecord/lib/active_record/
associations/has_many_through_association.rb:6:in initialize' from /Users/paulbrackenridge/Rails/projects/work/enabling development/ED/vendor/rails/activerecord/lib/active_record/ associations.rb:1048:innew’
from /Users/paulbrackenridge/Rails/projects/work/enabling
development/ED/vendor/rails/activerecord/lib/active_record/
associations.rb:1048:in `subjects’
from (irb):18

I’m not quite at home with the error messages yet, so if anyone could
give me a hand I would
be eternally greatfull :slight_smile:

Cheers,

P


#2

Right, managed to sort it (boy what a noobie mistake that was :frowning: )

I removed the pluralisation from the model names (god knows how I
managed
to add that!!) and added a the :source option to my :through clauses.
My
models now look something like this…

has_many :reviewerships has_many :reviewers, :through => :reviewerships, :source => :user

has_many :subjectships
has_many :subjects, :through => :subjectships, :source => :user