1

I want to be able to query user.items and item.owner, in code:

class Item < ActiveRecord::Base
  has_one :owner, :class_name => "User"
end

class User < ActiveRecord::Base
  # What goes here?
  has_many :items
end

I added to the Item model a column owner_id, how do I construct the User model so it uses owner_id instead of user_id when I call user.items?

foobar
  • 10,854
  • 18
  • 58
  • 66

1 Answers1

2

The item table need to contain a user_id column. If you want to set this key by hand then you should use:

... ,:foreign_key => 'owner_id'

Have a look at:

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

and

Defining foreign key relationships for Rails' models

Community
  • 1
  • 1
Roger
  • 7,535
  • 5
  • 41
  • 63