0

Here is my STI Models:

class User < ActiveRecord::Base
end

class Athlete < User
  has_many :sports, :through => :user_sports
  has_many :user_sports
end

class Coach < User
end

The UserSports table has user_id and sport_id... but then you run this:

athlete = Athlete.all.last
athlete.sports

The SQL that is generated is trying to use the athlete_id instead of the user_id... not too sure what I am doing wrong here... any suggestions would be greatful!

dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189

1 Answers1

0

I'm not sure why you have a UserSports table. You can just use a foreign key for either User or Sport, depending on their relation to each other.

The User model would need a specified relation to the Sport model, and vice versa.

More information on that is here: http://guides.rubyonrails.org/association_basics.html#the-has_many-association

It makes sense that it's trying to pull the athlete_id instead of the user_id, since you are calling on an Athlete object.

As a side note: There is no need to write Athlete.all.last - you only need to write Athlete.last.

CodeBiker
  • 2,985
  • 2
  • 33
  • 36