我有这种模式关系:俱乐部has_many Grounds has_many访问。俱乐部的主要关键是“club_id”,还有“clubname”字段。在访问中我有“club_home”和“club_away”,它们都是存储“club_id”的整数字段。而不是显示“club_id”我想要显示俱乐部的“俱乐部名称”字段。我怎样才能做到这一点?谢谢!
答案 0 :(得分:2)
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