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 6807023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:46:54+00:00 2026-05-26T19:46:54+00:00

I have 3 models, Coach, Team, and Event An event has attributes away_team and

  • 0

I have 3 models, “Coach”, “Team”, and “Event”

An event has attributes away_team and home_team, both of which belong to Team. A team belongs to a coach, and a coach can have many teams.

What I’d like to do is find all events where the home_team is not coached by a particular coach. So, something along the lines of:

Event.where("home_team.team.coach_id NOT ?", coach.id)

The problem is figuring out how to write up that syntax. I’m guessing I need to include the “team” model, but I’m not sure how to write an activerecord include that joins through a specific foreign key.

In short, any ideas on how to do this without getting SQL errors (which I’ve had countless of tonight) would be very appreciated.

Cheers.

  • 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. Editorial Team
    Editorial Team
    2026-05-26T19:46:55+00:00Added an answer on May 26, 2026 at 7:46 pm

    If I understand you correctly you need this kind of sql:

    SELECT events.* FROM events JOIN team on events.home_team_id = teams.id WHERE teams.coach_id != coach_id
    

    so in rails it should looks something like that:

    Event.joins("join teams on events.home_team_id = teams.id").where("teams.coach_id != ?", coach_id)
    

    Of course I don’t know your exact table namings, so you should fit this solution to your problem.

    If you have in Event model association: belogns_to: home_team, you can use only joins(:home_team).where(….)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Models, Programme and Event, a programme has many events. I need
I have models Product and Category . Category can have many products. ( Product
I have models Page and Commit. Page has many Commits. But sometimes i need
I have models similar to the following: class Band(models.Model): name = models.CharField(unique=True) class Event(models.Model):
I have models that belong to some 'group' (Company class). I want to add
I have models club and course where a course belongs to a club and
In a serious Java GUI app, you'll have models behind many of your GUI
I have models called User , Activity , and Task and I can do
I have models Foo and Bar . Bar has column foo_id . When I
I have an application written using Prism 4.0. It has lots of Views which

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.