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
Tom Taylor wrote:
class Memberships < ActiveRecord::Base
belongs_to :members
belongs_to :activities
You want
belongs_to :member
belongs_to :activity
Thanks Mark, but I still get the same error. Any more suggestions?
Cheers,
Tom
Thanks, but alas, the same result...
Cheers,
Tom
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'
Doh! I hadn't renamed memberships.rb to membership.rb. All fixed,
thanks Mark!