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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:38:50+00:00 2026-05-23T04:38:50+00:00

I’ve hit something that I don’t understand how to model with Rails associations and

  • 0

I’ve hit something that I don’t understand how to model with Rails associations and neither STI nor polymorphism seem to address it.

I want to be able to access attributes from a join table via the collection that’s created by has_many :through.

In the code below, this means that I want to be able to access the name and description of a committee position via the objects in the .members collection but as far as I can see I can’t do that. I have to go through the original join table.

e.g. modelling a club and it’s committee members

class User < ActiveRecord::Base
  attr_accessible :full_name, 
                  :email
  has_many: committee_positions
  has_many: committees, :through => committee_positions
end

class Committee < ActiveRecord::Base
  attr_accessible :name

  has_many :committee_positions
  has_many :members, :through => :committee_positions
end

class CommitteePosition < ActiveRecord::Base
  attr_accessible :user_id, 
                  :committee_id, 
                  :member_description, 
                  :role_title

  belongs_to :committee
  belongs_to :user
end

Assume that each committee position instance has a unique description
i.e. the description is particular to both the member and the committee and so has to be stored on the join table and not with either the user or the club.

e.g.

Committee member:  Matt Wilkins
Role:             "Rowing club president"
Description:      "Beats the heart of the rowing club to his own particular drum"

Is there a way to access the data in the join table via the committee.members collection?

While active record gives us this great alias for going directly to the members, there doesn’t seem to be any way to access the data on the join table that created the collection:

I cannot do the following:

rowing_committee.members.find_by_role_title('president').name

Each item in the .members collection is a user object and doesn’t seem to have access to either the role or description that’s stored in the CommitteePositions join table.

The only way to do this would be:

rowing_committee.committee_positions.find_by_role_title('president').user.name

This is perfectly do-able but is clunky and unhelpful. I feel like the use-case is sufficiently generic that I may well be missing something.

What I would like to access via objects in the committee.members collection

member
- full_name
- email
- role_title (referenced from join table attributes)
- member_description (referenced from join table attributes)

This is only a small thing but it feels ugly. Is there a clean way to instruct the "member" objects to inherit the information contained within the join table?

————– addendum

On working through this I realise that I can get half way to solving the problem by simply defining a new class for committee member and referencing that instead of user in the has_many :through relationship. It works a little bit better but is still pretty clunky

class Committee < ActiveRecord::Base
  ...

  has_many :committee_positions
  has_many :members, 
           :through => :committee_positions, 
           :class_name => 'CommitteeMember'
  ...
end


class CommitteeMember < User

  def description( committee )
    self.committees.find_by_committee_id( committee.id ).description
  end

  def role( committee )
    self.committees.find_by_committee_id( committee.id ).description
  end
end

Now this is getting closer but it still feels clunky in that the code to use it would be:

committee = Committee.first
president_description = committee.members.find_by_title('president').description( committee )

Is there any way to initialize these objects with the committee they are referencing?

  • 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-23T04:38:51+00:00Added an answer on May 23, 2026 at 4:38 am

    I think you could use some delegation here. In your Committee_Position class:

    class Committee_Position < ActiveRecord::Base
      attr_accessible :user_id, 
                    :committee_id, 
                    :member_description, 
                    :role_title
    
    belongs_to :committee
    belongs_to :user
    
    delegate :name, :email, :to => :user
    end
    

    so you could do what you say you want:

    rowing_club.committee_members.find_by_role_title('president').name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
I need a function that will clean a strings' special characters. I do NOT

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.