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.
Rasmus N. (Guest)
on 2009-01-29 19: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
Rob B. (Guest)
on 2009-01-29 19: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)
Julian L. (Guest)
on 2009-01-29 21: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 B. <removed_email_address@domain.invalid>
Rasmus N. (Guest)
on 2009-01-30 15:20
Rob B. 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 :-)
Rasmus N. (Guest)
on 2009-01-30 15:21
Julian L. 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 B. <removed_email_address@domain.invalid>

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.
Wouter d. (Guest)
on 2009-01-30 16:01
Rasmus N. 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?
Michael (Guest)
on 2009-02-02 00: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 N. <removed_email_address@domain.invalid>
Rasmus N. (Guest)
on 2009-02-02 09:48
Wouter de Bie wrote:
> Rasmus N. 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?
Julian L. (Guest)
on 2009-02-02 12: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 N.
<removed_email_address@domain.invalid
Rasmus N. (Guest)
on 2009-02-02 13:20
Julian L. 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 N.
> <removed_email_address@domain.invalid

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 ;-)
Ryan B. (Guest)
on 2009-02-02 22:34
(Received via mailing list)
If you set self.inheritance_column = nil on the model this may work.
Rasmus N. (Guest)
on 2009-02-04 11:22
Ryan B. 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.