STI and has_many :through not playing nicely

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

On 7 Apr, 01:45, Mark Reginald J. [email protected] wrote:

You want

 belongs_to :member
 belongs_to :activity

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

Cheers,

Tom

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.

On 7 Apr, 10:24, Mark Reginald J. [email protected] wrote:

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

Thanks, but alas, the same result…

Cheers,

Tom

Tom T. wrote:

On 7 Apr, 01:45, Mark Reginald J. [email protected] 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.

On 7 Apr, 10:39, “Tom T.” [email protected] wrote:

The full stack trace is:

Doh! I hadn’t renamed memberships.rb to membership.rb. All fixed,
thanks Mark!

On 7 Apr, 10:35, “Tom T.” [email protected] 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’