Forum: Ruby on Rails :through difficulties

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.
will (Guest)
on 2007-05-06 09:46
(Received via mailing list)
I'm trying to use :through on a many-to-many relationship.

I've got the following three models:

  class User < ActiveRecord::Base
    has_many :authors
    has_many :submissions, :through => :authors

  class Author < ActiveRecord::Base
    belongs_to :submission
    belongs_to :user

  class Submission < ActiveRecord::Base
    has_many :authors
    has_many :users, :through => :authors

I've also got test data for each of the models, but when I try to run
either of the following in script/console I get the subsequent error:

  user.submissions
  submission.users

  NameError: undefined local variable or method

Any suggestions on how to figure out what I've done wrong?
Jamal S. (Guest)
on 2007-05-06 14:36
Have the same problem...

http://www.ruby-forum.com/topic/107123
Pete Y. (Guest)
on 2007-05-08 05:09
(Received via mailing list)
On 06/05/2007, at 3:44 PM, will wrote:

>     belongs_to :user
>
>   class Submission < ActiveRecord::Base
>     has_many :authors
>     has_many :users, :through => :authors

Do you have all the correct fields on these tables to support the
relationships?

> I've also got test data for each of the models, but when I try to run
> either of the following in script/console I get the subsequent error:
>
>   user.submissions
>   submission.users
>
>   NameError: undefined local variable or method

Can you post an exact log of what you're typing in the console? I
suspect you're just misusing the relationships somehow.

Cheers,

Pete Y.
http://notahat.com/
This topic is locked and can not be replied to.