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>
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@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to