Forum: Ruby on Rails Noobie question about has_many :through

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
pbrackenridge (Guest)
on 2007-08-02 18:29
(Received via mailing list)
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 <a href="http://pastie.caboo.se/
84359">here</a> and models ca be
found <a href="http://pastie.caboo.se/84360"></a>

I tried the following in script/console

<code>
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)
</code>

but when I tried  <code>quest1.subjects</code>

I get the following error
<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:in `compute_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:in `klass'
        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:in `collect'
        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:in `check_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:in `new'
        from /Users/paulbrackenridge/Rails/projects/work/enabling
development/ED/vendor/rails/activerecord/lib/active_record/
associations.rb:1048:in `subjects'
        from (irb):18
</error>

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

Cheers,

P
pbrackenridge (Guest)
on 2007-08-02 19:13
(Received via mailing list)
Right, managed to sort it (boy what a noobie mistake that was :-( )

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...

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

  has_many :subjectships
  has_many :subjects, :through => :subjectships, :source => :user
</code>
This topic is locked and can not be replied to.