Rails 3 has_one with join table

时间:2013-05-06 20:35:04

标签: ruby-on-rails-3 has-one

我有以下内容:

has_many :sports, :through => :user_sports
has_one :primary_sport, class_name: "UserSport", conditions: ["user_sports.primary = ?", true]
has_many :user_sports

当我在控制台中运行时:

athlete = Athlete.all.last
athlete.primary_sport

返回的记录是连接表中的记录,而不是加入体育表的记录。有什么方法可以从加入中返回实际的运动?

1 个答案:

答案 0 :(得分:0)

您可能会这样做:

class UserSport < ActiveRecord::Base
  has_many :athletes
  has_many :sports
end


athlete = Athlete.all.last
athlete.primary_sport.sport

没有亲自尝试,只需检查并查看:)