niciliketo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2009-Apr-28 12:57 UTC
Single Table Inheritance - Basic
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?
Did you run the database migration? 2009/4/28 niciliketo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <niciliketo-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>> > 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? > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Apr 28, 1:57 pm, "nicilik...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <nicilik...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > 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?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
niciliketo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2009-Apr-28 14:24 UTC
Re: Single Table Inheritance - Basic
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; On Apr 28, 2:26 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Did you run the database migration? > > 2009/4/28 nicilik...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <nicilik...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> > > > > > 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?