John
John

Reputation: 6622

Foreign key with different name

I have this model relationship: Club has_many Grounds has_many Visits. The primary key of Club is "club_id" with also the field "clubname". In Visit I have "club_home" and "club_away" which both are integer fields storing a "club_id". Instead of showing the "club_id" I want to show the field "clubname" from Club. How can I do this? Thanks!

Upvotes: 0

Views: 267

Answers (1)

fl00r
fl00r

Reputation: 83680

class Visit < ActiveRecord::Base
  belongs_to :club_home, :foreign_key => :club_home, :class_name => "Club"
  belongs_to :club_away, :foreign_key => :club_away, :class_name => "Club"
end

UPD

@visit.club_home.clubname
@visit.club_away.clubname

Upvotes: 2

Related Questions