Belongs_to :type, :class_name => "ItemType"

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! :slight_smile:

  • Rasmus

‘type’ is used by ActiveRecord to indicate Single Table Inheritance.

That’s probably tripping you up (esp. since changing the name
eliminates the issue)

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. [email protected]

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. [email protected]

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.

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 :slight_smile:

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?

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. [email protected]

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! :slight_smile:

Should I file this surprising behavior as a bug?

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.
<[email protected]

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 :wink:

If you set self.inheritance_column = nil on the model this may work.

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.
<[email protected]

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” :slight_smile: