5

在我的应用程序中有Athletes......运动员可以有很多运动。

运动员:

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

用户运动:

class UserSport < ActiveRecord::Base
  attr_accessible :athlete_id, :sport_id, :primary

  belongs_to :athlete
  belongs_to :sport
end

我试图能够将primary_sport作为运动对象而不是对象拉回user_sport

4

1 回答 1

6

由于您通过 user_sports 拉动您的运动对象,因此您也应该通过 user_sports 拉动您的 primary_sport 对象。

has_one :primary_sport, :through => :user_sports, conditions: ["user_sports.primary = ?", true], class_name: "Sport"
于 2013-05-09T18:42:50.287 回答