rasmus
January 29, 2009, 6:13pm
1
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
January 29, 2009, 6:21pm
2
‘type’ is used by ActiveRecord to indicate Single Table Inheritance.
That’s probably tripping you up (esp. since changing the name
eliminates the issue)
rasmus
January 29, 2009, 8:45pm
3
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]
rasmus
January 30, 2009, 2:21pm
4
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.
rasmus
January 30, 2009, 2:20pm
5
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
January 30, 2009, 3:01pm
6
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?
rasmus
February 1, 2009, 11:55pm
7
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]
rasmus
February 2, 2009, 8:48am
8
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?
rasmus
February 2, 2009, 12:20pm
9
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:
Object#type is being deprecated (use #class instead)
I cannot find a synonym to type that does describe the entity as well
as “type” does
It works flawlessly
rasmus
February 2, 2009, 9:34pm
10
If you set self.inheritance_column = nil on the model this may work.
rasmus
February 2, 2009, 11:58am
11
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]
rasmus
February 4, 2009, 10:22am
12
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”