Rails 3 has_one with join table

问题描述 投票:0回答:1

我有以下内容:

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

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

ruby-on-rails-3 has-one
1个回答
0
投票

你可能会做这样的事情:

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


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

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

© www.soinside.com 2019 - 2024. All rights reserved.