Forum: Ruby on Rails Single Table Inheritance - Basic

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F76c75a8d95306d3e85d2c76ee5f9682?d=identicon&s=25 niciliketo@gmail.com (Guest)
on 2009-04-28 15:01
(Received via mailing list)
Hi,

I am struggling with a simple Single table Inheritance example.

I have two models, generated as follows:
script/generate scaffold Contact type:string created_at:datetime
updated_at:datetime
script/generate scaffold Person fname:string lname:string

I amended the Person model:

class Person < Contact
end


I then expected to be able to create a Person, with a fname, but this
fails (the following is from script/console):-

>> @person = Person.new => #<Person id: nil, type: "Person", created_at: nil, updated_at: 
nil>
>> @person.fname = "niciliketo"
NoMethodError: undefined method `fname=' for #<Person:0xb6e8e5fc>
        from /home/nic/site/crm/vendor/rails/activerecord/lib/
active_record/attribute_methods.rb:251:in `method_missing'
        from (irb):2
>>

The examples I can find are pretty terse, and don't seem to explain
why this isn't working.  Can anyone tell me why this happens?
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-04-28 15:26
(Received via mailing list)
Did you run the database migration?

2009/4/28 niciliketo@gmail.com <niciliketo@googlemail.com>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-04-28 15:39
(Received via mailing list)
On Apr 28, 1:57 pm, "nicilik...@gmail.com" <nicilik...@googlemail.com>
wrote:
>         from (irb):2
>
>
>
> The examples I can find are pretty terse, and don't seem to explain
> why this isn't working.  Can anyone tell me why this happens?

Because that's not quite what single table inheritance does. The
parent class is Contact, so instances of Person (and all other
subclasses of Contact) are rows in the contacts tables and thus have
the attributes that you specified when you created the contact model
(ie type and created_at)

Fred
F76c75a8d95306d3e85d2c76ee5f9682?d=identicon&s=25 niciliketo@gmail.com (Guest)
on 2009-04-28 16:25
(Received via mailing list)
Yes, I ran the db migrations after creating the scaffolding and
changing the models.

rake db:drop:all;
rake db:create:all;
rake db:migrate;
F76c75a8d95306d3e85d2c76ee5f9682?d=identicon&s=25 niciliketo@gmail.com (Guest)
on 2009-04-28 16:28
(Received via mailing list)
Thanks Percy,

Got it.  I guess the clue is in the SINGLE TABLE piece :-)

Apologies for my misunderstanding, and thanks for putting me straight.
This topic is locked and can not be replied to.