# STI and has\_many :through not playing nicely

**URL:** <https://www.ruby-forum.com/t/sti-and-has-many-through-not-playing-nicely/91336>\
**Category:** Rails\
**Created:** [April 7, 2007, 12:16am UTC](https://www.ruby-forum.com/t/sti-and-has-many-through-not-playing-nicely/91336 "2007-04-07T00:16:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [April 7, 2007, 12:16am UTC](https://www.ruby-forum.com/t/sti-and-has-many-through-not-playing-nicely/91336/1 "2007-04-07T00:16:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [April 7, 2007, 11:02am UTC](https://www.ruby-forum.com/t/sti-and-has-many-through-not-playing-nicely/91336/2 "2007-04-07T11:02:12Z")

</div>

On 7 Apr, 01:45, Mark Reginald J. [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [April 7, 2007, 2:47am UTC](https://www.ruby-forum.com/t/sti-and-has-many-through-not-playing-nicely/91336/3 "2007-04-07T02:47:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [April 7, 2007, 11:36am UTC](https://www.ruby-forum.com/t/sti-and-has-many-through-not-playing-nicely/91336/4 "2007-04-07T11:36:15Z")

</div>

On 7 Apr, 10:24, Mark Reginald J. [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [April 7, 2007, 11:26am UTC](https://www.ruby-forum.com/t/sti-and-has-many-through-not-playing-nicely/91336/5 "2007-04-07T11:26:14Z")

</div>

Tom T. wrote:

> On 7 Apr, 01:45, Mark Reginald J. [removed\_email\_address@domain.invalid](mailto: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.

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [April 7, 2007, 12:18pm UTC](https://www.ruby-forum.com/t/sti-and-has-many-through-not-playing-nicely/91336/6 "2007-04-07T12:18:25Z")

</div>

On 7 Apr, 10:39, “Tom T.” [removed\_email\_address@domain.invalid](mailto: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!

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [April 7, 2007, 11:40am UTC](https://www.ruby-forum.com/t/sti-and-has-many-through-not-playing-nicely/91336/7 "2007-04-07T11:40:15Z")

</div>

On 7 Apr, 10:35, “Tom T.” [removed\_email\_address@domain.invalid](mailto: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’
