hkelsey-EcXoqBC4F/kd5Ot1ntKM6ZqQE7yCjDx5@public.gmane.org
2005-Dec-19 17:54 UTC
Single Table Inheritance question
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 Ive 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.
On Dec 19, 2005, at 10:54 AM, hkelsey-EcXoqBC4F/kd5Ot1ntKM6ZqQE7yCjDx5@public.gmane.org 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 > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/railsDuane Johnson (canadaduane) http://blog.inquirylabs.com/
On Dec 19, 2005, at 11:31 AM, Duane Johnson wrote:> > On Dec 19, 2005, at 10:54 AM, hkelsey-EcXoqBC4F/kd5Ot1ntKM6ZqQE7yCjDx5@public.gmane.org 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 << pHrm. 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/
So, I have a model called Organization. Chapter and Committee inherit from it. I''m letting the user create new Organizations. So, they were able to select whether or not the Organization was a Chapter or Committee. When I tried to do stuff like <%= radio_button ''organization'', ''type %>, I got a ton of warning messages about how I should be using #class and not #type. And doing tests on the type field of an organization also reported a bunch of warnings. I was able to fix the whole thing by using a ''kind'' column (instead of ''type'') in the organizations table and using that as the inheritance column, but it seems weird that I had to do stuff like that. Any thoughts?
On 12/30/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So, I have a model called Organization. Chapter and Committee inherit from it. > > I''m letting the user create new Organizations. So, they were able to > select whether or not the Organization was a Chapter or Committee. > > When I tried to do stuff like <%= radio_button ''organization'', ''type > %>, I got a ton of warning messages about how I should be using #class > and not #type. And doing tests on the type field of an organization > also reported a bunch of warnings. > > I was able to fix the whole thing by using a ''kind'' column (instead of > ''type'') in the organizations table and using that as the inheritance > column, but it seems weird that I had to do stuff like that. >I''ve ended up doing the same. My base class is ''Rule'', so I went with rule_type as the inheritance column. While we''re here, does anyone have a better suggestion than this? I''ve got a postback-style action called new_rule, which contains a select box for the user to select which kind of ''rule'' they''d like, etc, etc. Because it''s a postback, there are no ''rule'' parameters the first time through. I''ve got something like this going, but I''m always paranoid about making things difficult for myself. def new_rule rule_type = ''Rule'' if params[:rule] rule_type = params[:rule][:rule_type] || ''Rule'' end klass = Object.const_get(rule_type) @rule = klass.new(params[:rule]) if request.post? and @rule.save flash[:notice] = "#{@rule.class} was successfully created." redirect_to :action => ''show_rule'', :id => @rule end end With this, @rule starts out as an instance of Rule, but ends up as something more specific, such as DomainRule, before being saved.
Hi, Joe, this is adressed in "agile web development with rails" as a less obvious constraint of the rails model of sti. The attribute type is also a name of a ruby method. therefore you shouldn''t adress it directly but via the objects indexing interface, using (in your case): organization[:type] = your radio_button parameter regards jan Joe Van Dyk wrote:>So, I have a model called Organization. Chapter and Committee inherit from it. > >I''m letting the user create new Organizations. So, they were able to >select whether or not the Organization was a Chapter or Committee. > >When I tried to do stuff like <%= radio_button ''organization'', ''type >%>, I got a ton of warning messages about how I should be using #class >and not #type. And doing tests on the type field of an organization >also reported a bunch of warnings. > >I was able to fix the whole thing by using a ''kind'' column (instead of >''type'') in the organizations table and using that as the inheritance >column, but it seems weird that I had to do stuff like that. > >Any thoughts? >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >
On 12/30/05, Wilson Bilkovich <wilsonb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 12/30/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > So, I have a model called Organization. Chapter and Committee inherit from it. > > > > I''m letting the user create new Organizations. So, they were able to > > select whether or not the Organization was a Chapter or Committee. > > > > When I tried to do stuff like <%= radio_button ''organization'', ''type > > %>, I got a ton of warning messages about how I should be using #class > > and not #type. And doing tests on the type field of an organization > > also reported a bunch of warnings. > > > > I was able to fix the whole thing by using a ''kind'' column (instead of > > ''type'') in the organizations table and using that as the inheritance > > column, but it seems weird that I had to do stuff like that. > > > I''ve ended up doing the same. My base class is ''Rule'', so I went with > rule_type as the inheritance column.Seems like that should be the default, instead of ''type''. The default choice seems broken (unless I''m understanding it wrong).
said that (obj.type as a deprecated synonym for Object#class in ruby: http://www.ruby-doc.org/core/classes/Object.html#M001065) your approach seems to me as a fine workaround. regards Jan Joe Van Dyk wrote:>So, I have a model called Organization. Chapter and Committee inherit from it. > >I''m letting the user create new Organizations. So, they were able to >select whether or not the Organization was a Chapter or Committee. > >When I tried to do stuff like <%= radio_button ''organization'', ''type >%>, I got a ton of warning messages about how I should be using #class >and not #type. And doing tests on the type field of an organization >also reported a bunch of warnings. > >I was able to fix the whole thing by using a ''kind'' column (instead of >''type'') in the organizations table and using that as the inheritance >column, but it seems weird that I had to do stuff like that. > >Any thoughts? >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >
On 12/30/05, Jan Prill <JanPrill@blauton.de> wrote:> Joe Van Dyk wrote: > > >So, I have a model called Organization. Chapter and Committee inherit from it. > > > >I''m letting the user create new Organizations. So, they were able to > >select whether or not the Organization was a Chapter or Committee. > > > >When I tried to do stuff like <%= radio_button ''organization'', ''type > >%>, I got a ton of warning messages about how I should be using #class > >and not #type. And doing tests on the type field of an organization > >also reported a bunch of warnings. > > > >I was able to fix the whole thing by using a ''kind'' column (instead of > >''type'') in the organizations table and using that as the inheritance > >column, but it seems weird that I had to do stuff like that. > > Hi, Joe, > > this is adressed in "agile web development with rails" as a less obvious > constraint of the rails model of sti. The attribute type is also a name > of a ruby method. therefore you shouldn''t adress it directly but via the > objects indexing interface, using (in your case): organization[:type] > your radio_button parameterHi, thanks for pointing that out. But, doing <%= radio_button "organization", "type" %> was causing a ton of warning messages. It''s not a big deal though. Anyone know why ''type'' was chosen as opposed to ''<class>_type''?