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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:57:08+00:00 2026-05-20T01:57:08+00:00

My app uses a :has_many :through association, and I’m having trouble figuring out how

  • 0

My app uses a :has_many :through association, and I’m having trouble figuring out how to most efficiently load and display data from both ends of the association and the association itself.

Here are my classes:

class Person < ActiveRecord::Base
  has_many :people_ranks
  has_many :ranks, :through => :people_ranks
  has_many :institutions_people
  has_many :institutions, :through => :institutions_people
  belongs_to :school
  belongs_to :department
end
class Institution < ActiveRecord::Base
  has_many :institutions_people
  has_many :people, :through => :institutions_people
end
class InstitutionsPerson < ActiveRecord::Base
  belongs_to :institution
  belongs_to :person
end

and their corresponding models:

    create_table :people, :force => true do |t|
      t.string :name
      t.string :degree
      t.integer :year_grad
      t.integer :year_hired
    end
   create_table :institutions, :force => true do |t|
      t.string :name
      t.integer :ischool
    end
    create_table :institutions_people, :id => false do |t|
      t.integer :institution_id
      t.integer :person_id
      t.string :rel_type
    end

I want to show a person’s institution info with something like @person.year_hired, @person.institution.name, and @person.institution.institutions_people.rel_type (where rel_type is either “graduated” or “hired:), but I know that third part won’t work. Using the following in the show bit in the person_controller:

    @person = Person.find(params[:id], :include => [:school, :department, :institutions_people, :people_ranks, {:institutions_people => :institution}, {:people_ranks => :rank}])

gives me access to @person.institutions and @person.institutions_people, but how do I connect the rel_type attribute from the join to the person-institution relationship? (I’m coming from PHP and now how to build the SQL and loop through it there, but RoR has me stumped.)

I’ve looked for help under “eager loading” and “associations with :has_many :through”, but I get answers about building the associations. My question is really about accessing the association’s data after it exists. My app uses static data, and I’m not worried about the update, destroy, or create methods. Thank you for your help!

  • 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-20T01:57:09+00:00Added an answer on May 20, 2026 at 1:57 am

    The way to access the data is through the institutions_people association. So, you would do something like:

    me = Person.first
    rel = me.institutions_people.first
    

    And then in the view

    <%= rel.rel_type %> from <%= rel.institution.name %>
    

    Alternatively, you can give yourself a full list of institutions along with their info:

    me = Person.first
    

    And then in the view:

    <% for ip in me.institutions_people %>
      <%= ip.rel_type %> from <%= ip.institution.name %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: Our web app uses the jquery.constrain.js plugin to handle data entry in some
Im building a codeigniter app which uses json_encode to provide ajax data in many
I have a navigation based app that uses Core Data for storage. The schema
My ASP.NET MVC intranet app has a data repository that uses current user's Windows
My app uses an API from an external JAR file. This JAR file has
My app uses a WebRequest at certain points to get pages from itself. This
Our app uses a component that requires a license file in the directory with
My app uses javascript to generate all the HTML for my website. The index.html
My app uses a landscape only view and requires the user to answer a
My app uses a per-user session to allow multiple sessions from the same user

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.