Single Table Inheritance question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Single Table Inheritance question

hkelsey
Hello,

I'm having a heck of a time getting Single Table Inheritance to work in my app.
I have the following models and have scaffolded out Person and Contact.

person.rb
class Person < ActiveRecord::Base
  has_many :contacts
end

client.rb
class Client < Person
end

contact.rb
class Contact < ActiveRecord::Base
  belongs_to :person
end

phone.rb
class Phone < Contact
end

In script/console I’ve run:
 c = Client.find :first
 c.phones.create :contactdata => “555-555-5555”

and get “NoMethodError : undefined method ‘phones’ etc…”

I tried moving the relationships in to the client and phone models but then
there was an SQL error: “Unknown column contacts.client_id” because the
contacts table has a person_id column not a client_id column. Any help would be
greatly appreciated.

Hugh


----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: Single Table Inheritance question

Duane Johnson

On Dec 19, 2005, at 10:54 AM, [hidden email] wrote:

> Hello,
>
> I'm having a heck of a time getting Single Table Inheritance to  
> work in my app.
> I have the following models and have scaffolded out Person and  
> Contact.
>
> person.rb
> class Person < ActiveRecord::Base
>   has_many :contacts
> end
>
> client.rb
> class Client < Person
> end
>
> contact.rb
> class Contact < ActiveRecord::Base
>   belongs_to :person
> end
>
> phone.rb
> class Phone < Contact
> end
>
> In script/console I’ve run:
>  c = Client.find :first
>  c.phones.create :contactdata => “555-555-5555”
>
> and get “NoMethodError : undefined method ‘phones’ etc…”
>
You need to create a phone and then add it to the list of phones:

c = Client.find :first
p = Phone.create :contactdata => '555-555-5555'
c.phones << p

> I tried moving the relationships in to the client and phone models  
> but then
> there was an SQL error: “Unknown column contacts.client_id” because  
> the
> contacts table has a person_id column not a client_id column. Any  
> help would be
> greatly appreciated.
>
> Hugh
>
>
> ----------------------------------------------------------------
> This message was sent using IMP, the Internet Messaging Program.
>
> _______________________________________________
> Rails mailing list
> [hidden email]
> http://lists.rubyonrails.org/mailman/listinfo/rails

Duane Johnson
(canadaduane)
http://blog.inquirylabs.com/


_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: Single Table Inheritance question

Duane Johnson

On Dec 19, 2005, at 11:31 AM, Duane Johnson wrote:

>
> On Dec 19, 2005, at 10:54 AM, [hidden email] wrote:
>
>> Hello,
>>
>> I'm having a heck of a time getting Single Table Inheritance to  
>> work in my app.
>> I have the following models and have scaffolded out Person and  
>> Contact.
>>
>> person.rb
>> class Person < ActiveRecord::Base
>>   has_many :contacts
>> end
>>
>> client.rb
>> class Client < Person
>> end
>>
>> contact.rb
>> class Contact < ActiveRecord::Base
>>   belongs_to :person
>> end
>>
>> phone.rb
>> class Phone < Contact
>> end
>>
>> In script/console I’ve run:
>>  c = Client.find :first
>>  c.phones.create :contactdata => “555-555-5555”
>>
>> and get “NoMethodError : undefined method ‘phones’ etc…”
>>
> You need to create a phone and then add it to the list of phones:
>
> c = Client.find :first
> p = Phone.create :contactdata => '555-555-5555'
> c.phones << p

Hrm.  And it also appears that your relationship is actually  
has_many :contacts, so you'd do this instead:

c = Client.find :first
p = Phone.create :contactdata => '555-555-5555'
c.contacts << p


Duane Johnson
(canadaduane)
http://blog.inquirylabs.com/


_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails