Note that there are some explanatory texts on larger screens.

plurals
  1. POActiverecord association question: getting has_many :through to work
    text
    copied!<p>I'm building an app in Ruby on Rails, and I'm including 3 of my models (and their migration scripts) to show what I'm trying to do, and what isn't working. Here's the rundown: I have users in my application that belong to teams, and each team can have multiple coaches. I want to be able to pull a list of the coaches that are applicable to a user. </p> <p>For instance, User A could belong to teams T1 and T2. Teams T1 and T2 could have four different coaches each, and one coach in common. I'd like to be able to pull the list of coaches by simply saying: </p> <pre><code>u = User.find(1) coaches = u.coaches </code></pre> <p>Here are my migration scripts, and the associations in my models. Am I doing something incorrectly in my design? Are my associations correct?</p> <pre><code>class CreateUsers &lt; ActiveRecord::Migration def self.up create_table :users do |t| t.column :login, :string, :default =&gt; nil t.column :firstname, :string, :default =&gt; nil t.column :lastname, :string, :default =&gt; nil t.column :password, :string, :default =&gt; nil t.column :security_token, :string, :default =&gt; nil t.column :token_expires, :datetime, :default =&gt; nil t.column :legacy_password, :string, :default =&gt; nil end end def self.down drop_table :users end end class CreateTeams &lt; ActiveRecord::Migration def self.up create_table :teams do |t| t.column :name, :string end end def self.down drop_table :teams end end class TeamsUsers &lt; ActiveRecord::Migration def self.up create_table :teams_users, :id =&gt; false do |t| t.column :team_id, :integer t.column :user_id, :integer t.column :joined_date, :datetime end end def self.down drop_table :teams_users end end </code></pre> <p>Here are the models (not the entire file):</p> <pre><code>class User &lt; ActiveRecord::Base has_and_belongs_to_many :teams has_many :coaches, :through =&gt; :teams class Team &lt; ActiveRecord::Base has_many :coaches has_and_belongs_to_many :users class Coach &lt; ActiveRecord::Base belongs_to :teams end </code></pre> <p>This is what happens when I try to pull the coaches:</p> <pre><code>u = User.find(1) =&gt; #&lt;User id: 1, firstname: "Dan", lastname: "Wolchonok"&gt; &gt;&gt; u.coaches ActiveRecord::StatementInvalid: Mysql::Error: #42S22Unknown column 'teams.user_id' in 'where clause': SELECT `coaches`.* FROM `coaches` INNER JOIN teams ON coaches.team_id = teams.id WHERE ((`teams`.user_id = 1)) </code></pre> <p>Here's the error in sql:</p> <p>Mysql::Error: #42S22Unknown column 'teams.user_id' in 'where clause': SELECT <code>coaches</code>.* FROM <code>coaches</code> INNER JOIN teams ON coaches.team_id = teams.id WHERE ((<code>teams</code>.user_id = 1)) </p> <p>Am I missing something in my :through clause? Is my design totally off? Can someone point me in the right direction?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload