Forum: Ruby on Rails belongs_to :type, :class_name => "ItemType"

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.
1e782845563c051556143a36ee9cf365?d=identicon&s=25 Rasmus Nielsen (rasmus)
on 2009-01-29 18:13
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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-01-29 18:21
(Received via mailing list)
'type' is used by ActiveRecord to indicate Single Table Inheritance.

That's probably tripping you up (esp. since changing the name
eliminates the issue)
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-01-29 20:45
(Received via mailing list)
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@AgileConsultingLLC.com>
1e782845563c051556143a36ee9cf365?d=identicon&s=25 Rasmus Nielsen (rasmus)
on 2009-01-30 14:20
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 :-)
1e782845563c051556143a36ee9cf365?d=identicon&s=25 Rasmus Nielsen (rasmus)
on 2009-01-30 14:21
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@AgileConsultingLLC.com>

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.
Bde8d8f24c1ca83f452b5a9d03d5ead6?d=identicon&s=25 Wouter de Bie (xinit)
on 2009-01-30 15:01
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?
8277ed5c55542ead8ec7724c42967534?d=identicon&s=25 Michael (Guest)
on 2009-02-01 23:55
(Received via mailing list)
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...@andreas-s.net>
1e782845563c051556143a36ee9cf365?d=identicon&s=25 Rasmus Nielsen (rasmus)
on 2009-02-02 08:48
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?
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-02 11:58
(Received via mailing list)
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@andreas-s.net
1e782845563c051556143a36ee9cf365?d=identicon&s=25 Rasmus Nielsen (rasmus)
on 2009-02-02 12:20
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@andreas-s.net

I 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 ;-)
9a2a53db8e9b4476038c94a64b32833f?d=identicon&s=25 Ryan Bigg (ryan-bigg)
on 2009-02-02 21:34
(Received via mailing list)
If you set self.inheritance_column = nil on the model this may work.
1e782845563c051556143a36ee9cf365?d=identicon&s=25 Rasmus Nielsen (rasmus)
on 2009-02-04 10:22
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" :)
This topic is locked and can not be replied to.