Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 62857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:32:30+00:00 2026-05-10T18:32:30+00:00

I’m building an app in Ruby on Rails, and I’m including 3 of my

  • 0

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.

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:

u = User.find(1) coaches = u.coaches 

Here are my migration scripts, and the associations in my models. Am I doing something incorrectly in my design? Are my associations correct?

class CreateUsers < ActiveRecord::Migration   def self.up     create_table :users do |t|       t.column :login, :string, :default => nil       t.column :firstname, :string, :default => nil       t.column :lastname, :string, :default => nil       t.column :password, :string, :default => nil       t.column :security_token, :string, :default => nil       t.column :token_expires, :datetime, :default => nil       t.column :legacy_password, :string, :default => nil     end   end    def self.down     drop_table :users   end end  class CreateTeams < 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 < ActiveRecord::Migration   def self.up     create_table :teams_users, :id => 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 

Here are the models (not the entire file):

class User < ActiveRecord::Base    has_and_belongs_to_many :teams   has_many :coaches, :through => :teams  class Team < ActiveRecord::Base   has_many :coaches   has_and_belongs_to_many :users  class Coach < ActiveRecord::Base   belongs_to :teams end 

This is what happens when I try to pull the coaches:

u = User.find(1) => #<User id: 1, firstname: 'Dan', lastname: 'Wolchonok'> >> 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))  

Here’s the error in sql:

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))

Am I missing something in my :through clause? Is my design totally off? Can someone point me in the right direction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:32:30+00:00Added an answer on May 10, 2026 at 6:32 pm

    You can’t do a has_many :through twice in a row. It’ll tell you that its an invalid association. If you don’t want to add finder_sql like above, you can add a method that mimics what you’re trying to do.

      def coaches     self.teams.collect do |team|       team.coaches     end.flatten.uniq   end 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer An even better solution might be to use a function… May 11, 2026 at 1:58 pm
  • added an answer unless (caller) { print 'This is the script being executed\n';… May 11, 2026 at 1:58 pm
  • added an answer Try this: for /f 'usebackq delims=' %X in (`dir /b`)… May 11, 2026 at 1:58 pm

Related Questions

I keep getting tasks that are above my skill level. How can I address this without coming accross as grossly incompetent?
I have a web-service that I will be deploying to dev, staging and production.
I'm thinking of starting a wiki, probably on a low cost LAMP hosting account.
I have the following tables in my database that have a many-to-many relationship, which
I'm using the RESTful authentication Rails plugin for an app I'm developing. I'm having
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.