Forum: Ruby on Rails NameError when trying to combine Recipe 14 (rich HABTM) & Re

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Alain R. (Guest)
on 2006-03-12 23:56
(Received via mailing list)
Hi all,

I'm trying to model self-referential relations between people, where the
relation has attached data.
     John 'works for' Bob

Problem: ':through' doesn't work, and generates a NameError.

=> the models :
     Person    (attr: name)
     Relation  (attr: source_id, target_id)
       (more details below)

ex: John 'works for' Bob
1>   jsmith = Person.create(:name => 'John S.')
2>   jdoe   = Person.create(:name => 'John D.')
3>   jsmith.to_relations.create(:name => "works_for", :target => jdoe)
4>   assert_equal 1, jsmith.slaves.size
     NameError: activesupport/lib/active_support/dependencies.rb:106:in
`const_missing': uninitialized constant Slafe


   class Person < ActiveRecord::Base
       has_many    :to_relations  , :foreign_key => 'source_id',
:class_name => 'Relation'
       has_many    :slaves , :through => :to_relations   <<---- DOES NOT
WORK !!!!!!!

   #  id                  :integer(11)   not null
   #  source_id           :integer(11)
   #  target_id           :integer(11)
   #  name                :string(255)
   class Relation < ActiveRecord::Base
       belongs_to  :target ,  :foreign_key => 'target_id', :class_name
=> 'Person'

Any idea?

This topic is locked and can not be replied to.