Reputation: 48453
I have these two tables:
colors
- id
- name
- name_code
cars
- id
- manufacturer
- color_code_name
(in the columns colors.name_code
and cars.color_code_name
are the same values)
I would like to get all cars with the respective color. I tried to set up these associations:
class Color < ActiveRecord::Base
has_many :cars, :primary_key => "name_code"
end
class Car < ActiveRecord::Base
belongs_to :color, :primary_key => "color_code_name"
end
But this is unfortunately doesn't working... I didn't specify yet this kind of association, so I am not really sure, if it's possible to do it...
I am gonna be grateful for every advise
Upvotes: 0
Views: 145
Reputation: 21
Need to add an = in the primary key assignment (for rails 3.1 anyway)
class Color < ActiveRecord::Base
self.primary_key = :name_code
has_many :cars, :foreign_key => :color_code_name
end
class Car < ActiveRecord::Base
self.primary_key = :color_code_name
belongs_to :color, :foreign_key => :color_code_name
end
Thanks for the answer. It was what I was looking for
Upvotes: 0
Reputation: 3919
Try this:
class Color < ActiveRecord::Base
self.primary_key :name_code
has_many :cars, :foreign_key => :color_code_name
end
class Car < ActiveRecord::Base
self.primary_key :color_code_name
belongs_to :color, :foreign_key => :color_code_name
end
Upvotes: 1