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

  • SEARCH
  • Home
  • 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 7985399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:32:36+00:00 2026-06-04T11:32:36+00:00

How do I set up a belongs_to :through (which I know is not valid)

  • 0

How do I set up a belongs_to :through (which I know is not valid) relationship? For instance: A company has many departments. A department has many teams. And some teams are cross-functional so they can span many departments (habtm).

class Company < ActiveRecord::Base
  has_many :departments
  has_many :teams, through: :departments
end

class Department < ActiveRecord::Base
  belongs_to :company;
  has_and_belongs_to_many :teams
end

class Team < ActiveRecord::Base
  has_and_belongs_to_many :departments
end

How do I get company from team. What is a good way to do this? The first should work but can I or should I be trying to do it in the model as a relationship?

class Team < ActiveRecord::Base
  has_and_belongs_to_many :departments

  def company 
    departments.first.company
  end

end

or

class Team < ActiveRecord::Base
  has_and_belongs_to_many: departments

  has_one :company, through: departments  (<-- is this valid?, seems like this should be has_many but that's not right!)
end
  • 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-06-04T11:32:38+00:00Added an answer on June 4, 2026 at 11:32 am

    The relationship you want is already there. If you know all the departments belong to the same company you can do

    my_team.departments.first.company
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a many-to-many relationship set up through a join model. Essentially, I allow
I have a has_many :through relationship set up like so class Situation < ActiveRecord::Base
Rails has a has_one :through association that helps set up a one-to-one association with
I have a typical many-to-many relationship using has_many => :through , as detailed below.
I have a simple has_many through relationship set up: class Tag < ActiveRecord::Base has_many
I have the following models set up: class Contact < ActiveRecord::Base belongs_to :band belongs_to
Set rs = conn.Execute(Statement) //rs has 6 fields I want to add the current
I don't know squat about TFS, other than as a user who has performed
Once again, I have my form_for for my Order model, which has_many :services, :through
So Rails doesn't have support for :through associations through a habtm relationship. There are

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.