Hi there, I have a Item-model that belongs to the ItemType-model. This works fine: class Item < ActiveRecord::Base belongs_to :item_type end However, if I rewrite this to: class Item < ActiveRecord::Base belongs_to :type, :class_name => "ItemType" end ... i get the following in the console:>> Item.first.type=> nil When looking at my log I can see that no query are sent to the db-server. This does not work: class Item < ActiveRecord::Base belongs_to :blah, :class_name => "ItemType" end However, this does: class Item < ActiveRecord::Base belongs_to :item_type, :class_name => "ItemType" # I know this does not make sense end Can anybody explain what''s going on there? Ultimately I would like to be able to write Item.find(x).type. Thanks in advance! :-) - Rasmus -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
''type'' is used by ActiveRecord to indicate Single Table Inheritance. That''s probably tripping you up (esp. since changing the name eliminates the issue) On Jan 29, 2009, at 12:13 PM, Rasmus Nielsen wrote:> > Hi there, > > I have a Item-model that belongs to the ItemType-model. This works > fine: > > class Item < ActiveRecord::Base > belongs_to :item_type > end > > However, if I rewrite this to: > > class Item < ActiveRecord::Base > belongs_to :type, :class_name => "ItemType" > end > > ... i get the following in the console: >>> Item.first.type > => nil > > When looking at my log I can see that no query are sent to the > db-server. This does not work: > class Item < ActiveRecord::Base > belongs_to :blah, :class_name => "ItemType" > end > > However, this does: > class Item < ActiveRecord::Base > belongs_to :item_type, :class_name => "ItemType" # I know this does > not make sense > end > > Can anybody explain what''s going on there? Ultimately I would like > to be > able to write Item.find(x).type. > > Thanks in advance! :-) > > - Rasmus > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
At the ruby level, type is an alias of class, too (sorta), so it''s gonna have some major issues with that word. Sent from my iPhone On 30/01/2009, at 4:20 AM, Rob Biedenharn <Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> > ''type'' is used by ActiveRecord to indicate Single Table Inheritance. > > That''s probably tripping you up (esp. since changing the name > eliminates the issue) > > On Jan 29, 2009, at 12:13 PM, Rasmus Nielsen wrote: > >> >> Hi there, >> >> I have a Item-model that belongs to the ItemType-model. This works >> fine: >> >> class Item < ActiveRecord::Base >> belongs_to :item_type >> end >> >> However, if I rewrite this to: >> >> class Item < ActiveRecord::Base >> belongs_to :type, :class_name => "ItemType" >> end >> >> ... i get the following in the console: >>>> Item.first.type >> => nil >> >> When looking at my log I can see that no query are sent to the >> db-server. This does not work: >> class Item < ActiveRecord::Base >> belongs_to :blah, :class_name => "ItemType" >> end >> >> However, this does: >> class Item < ActiveRecord::Base >> belongs_to :item_type, :class_name => "ItemType" # I know this does >> not make sense >> end >> >> Can anybody explain what''s going on there? Ultimately I would like >> to be >> able to write Item.find(x).type. >> >> Thanks in advance! :-) >> >> - Rasmus >> -- >> Posted via http://www.ruby-forum.com/. >> >>> > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn wrote:> ''type'' is used by ActiveRecord to indicate Single Table Inheritance. > > That''s probably tripping you up (esp. since changing the name > eliminates the issue)As far as I know this is only the issue when you name a column "type" -which is not what I''ve done here. Thanks anyway :-) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Julian Leviston wrote:> At the ruby level, type is an alias of > class, too (sorta), so it''s gonna have some major issues with that word. > > Sent from my iPhone > > On 30/01/2009, at 4:20 AM, Rob Biedenharn <Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org>Yes I know - but why doesn''t belongs_to :blah, :class_name => "ItemType" ... not work then? It seems it fails only when the class name ends with *Type which does really makes sense to me. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rasmus Nielsen wrote:> Yes I know - but why doesn''t > > belongs_to :blah, :class_name => "ItemType" > > ... not work then?Does it work when you explicitly define the foreign_key? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Rasmus, I ran into a similar thing - I found I needed to specify what the foreign key for ItemType was in the Item table. In my instance class Assessment < ActiveRecord::Base belongs_to :created_by, :class_name => "User" wouldn''t work but below worked fine: class Assessment < ActiveRecord::Base belongs_to :created_by, :class_name => "User", :foreign_key => "created_by" Hope it helps! Michael. On Jan 30, 4:13 am, Rasmus Nielsen <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi there, > > I have a Item-model that belongs to the ItemType-model. This works fine: > > class Item < ActiveRecord::Base > belongs_to :item_type > end > > However, if I rewrite this to: > > class Item < ActiveRecord::Base > belongs_to :type, :class_name => "ItemType" > end > > ... i get the following in the console:>> Item.first.type > > => nil > > When looking at my log I can see that no query are sent to the > db-server. This does not work: > class Item < ActiveRecord::Base > belongs_to :blah, :class_name => "ItemType" > end > > However, this does: > class Item < ActiveRecord::Base > belongs_to :item_type, :class_name => "ItemType" # I know this does > not make sense > end > > Can anybody explain what''s going on there? Ultimately I would like to be > able to write Item.find(x).type. > > Thanks in advance! :-) > > - Rasmus > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Wouter de Bie wrote:> Rasmus Nielsen wrote: > >> Yes I know - but why doesn''t >> >> belongs_to :blah, :class_name => "ItemType" >> >> ... not work then? > > > Does it work when you explicitly define the foreign_key?Xinit and Michael: thank you. It worked after specifying the foreign key explicitly. This saved me a big headache! Thank you very much! :-) Should I file this surprising behavior as a bug? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
It''s bad practice to have an association or method called type because ruby already defines One IMHO Sent from my iPhone On 02/02/2009, at 6:48 PM, Rasmus Nielsen <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > wrote:> > Wouter de Bie wrote: >> Rasmus Nielsen wrote: >> >>> Yes I know - but why doesn''t >>> >>> belongs_to :blah, :class_name => "ItemType" >>> >>> ... not work then? >> >> >> Does it work when you explicitly define the foreign_key? > > Xinit and Michael: thank you. > > It worked after specifying the foreign key explicitly. This saved me a > big headache! Thank you very much! :-) > > Should I file this surprising behavior as a bug? > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Julian Leviston wrote:> It''s bad practice to have an association or method called type because > ruby already defines > One IMHO > > Sent from my iPhone > > On 02/02/2009, at 6:48 PM, Rasmus Nielsen > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.orgI agree - but I''ll use it anyway because of the following reasons: 1) Object#type is being deprecated (use #class instead) 2) I cannot find a synonym to type that does describe the entity as well as "type" does 3) It works flawlessly ;-) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
If you set self.inheritance_column = nil on the model this may work. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ryan Bigg wrote:> If you set self.inheritance_column = nil on the model this may work.Thank you, but it does not seem to work. The only thing that I can get working is setting the foreign_key explicitly, which is "good enough" :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---