Forum: Ruby on Rails STI and has_many :through not playing nicely

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.
Tom T. (Guest)
on 2007-04-07 02:16
(Received via mailing list)
Hi all,

I'm trying to use STI (single table inheritance) with
has_many :through, and I'm getting a NameError: uninitialized
constant. Details below. Presumably I've set up the relationship
incorrectly? Any help would be greatly appreciated!

I have the following models:

class Activity < ActiveRecord::Base

  has_many :memberships
  has_many :members, :through => :memberships

class Branch < Activity

class Member < ActiveRecord::Base

  has_many :memberships
  has_many :activities, :through => :memberships

class Memberships < ActiveRecord::Base

  belongs_to :members
  belongs_to :activities

I'm trying to run the following test (the activity fixture I'm testing
is a branch):

class ActivityTest < Test::Unit::TestCase
  fixtures :activities, :members, :memberships

  # Replace this with your real tests.
  def test_has_member
    assert_equal activities(:bristol).members.empty?, false
  end
end

and it's failing with:

NameError: uninitialized constant Activity::Membership
Mark Reginald J. (Guest)
on 2007-04-07 04:47
(Received via mailing list)
Tom T. wrote:

> class Memberships < ActiveRecord::Base
>
>   belongs_to :members
>   belongs_to :activities

You want

     belongs_to :member
     belongs_to :activity

--
We develop, watch us RoR, in numbers too big to ignore.
Tom T. (Guest)
on 2007-04-07 13:02
(Received via mailing list)
On 7 Apr, 01:45, Mark Reginald J. <removed_email_address@domain.invalid> wrote:
> You want
>
>      belongs_to :member
>      belongs_to :activity

Thanks Mark, but I still get the same error. Any more suggestions?

Cheers,

Tom
Mark Reginald J. (Guest)
on 2007-04-07 13:26
(Received via mailing list)
Tom T. wrote:
> On 7 Apr, 01:45, Mark Reginald J. <removed_email_address@domain.invalid> wrote:
>> You want
>>
>>      belongs_to :member
>>      belongs_to :activity
>
> Thanks Mark, but I still get the same error. Any more suggestions?

Yes, it should also be "class Membership" rather than "class
Memberships".

--
We develop, watch us RoR, in numbers too big to ignore.
Tom T. (Guest)
on 2007-04-07 13:36
(Received via mailing list)
On 7 Apr, 10:24, Mark Reginald J. <removed_email_address@domain.invalid> wrote:
> Yes, it should also be "class Membership" rather than "class Memberships".

Thanks, but alas, the same result...

Cheers,

Tom
Tom T. (Guest)
on 2007-04-07 13:40
(Received via mailing list)
On 7 Apr, 10:35, "Tom T." <removed_email_address@domain.invalid> wrote:
> Thanks, but alas, the same result...

The full stack trace is:

  1) Error:
test_has_member(ActivityTest):
NameError: uninitialized constant Activity::Membership
    /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:477:in `const_missing'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
base.rb:1360:in `compute_type'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
reflection.rb:125:in `klass'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
reflection.rb:177:in `source_reflection'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
reflection.rb:177:in `source_reflection'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
reflection.rb:186:in `check_validity!'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
associations/has_many_through_association.rb:6:in `initialize'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
associations.rb:934:in `members'
    ./test/unit/activity_test.rb:7:in `test_has_member'
Tom T. (Guest)
on 2007-04-07 14:18
(Received via mailing list)
On 7 Apr, 10:39, "Tom T." <removed_email_address@domain.invalid> wrote:
> The full stack trace is:

Doh! I hadn't renamed memberships.rb to membership.rb. All fixed,
thanks Mark!
This topic is locked and can not be replied to.