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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:57:15+00:00 2026-05-13T19:57:15+00:00

I have a typical many-to-many relationship using has_many => :through , as detailed below.

  • 0

I have a typical many-to-many relationship using has_many => :through, as detailed below.

class member
  has_many member_roles
  has_many roles, :through => :member_roles
end

class role
  has_many member_roles
  has_man members, :through => :member_roles
end

class member_role
  belongs_to :member
  belongs_to :role
  # has following fields: member_id, role_id, scope, sport_id
end

What I’m trying to do here is allow members to be assigned roles. Each member role has a scope, which by default is set to “All” but if desired can be set to “Sport”. If the scope is set to sport then we also capture the sport_id, which allows us to restrict assess on that role to a particular sport (ie, can only manage the teams of that sport, rather than the teams of every sport). Sounds simple enough.

I have setup my update_member_roles action something like this:

def update

  # Assume we passing a param like: params[:member][:roles]
  # as an array of hashes consisting of :role_id and if set, :sport_id

  roles = (params[:member] ||= {}).delete "roles"
  @member.roles = Role.find_all_by_id(roles.map{|r| r["role_id"]})
  if @member.update_attributes params[:member]
    flash[:notice] = "Roles successfully updated."
    redirect_to member_path(@member)
  else
    render :action => "edit"
  end
end

The above works nice enough, it sets the appropriate member_roles very nicely… but as I’m working on the Role model and not the MemberRole model I’m kind of stuck as to how I can access the joining model to set the :scope and :sport_id.

Any pointers here would be much appreciated.

  • 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-13T19:57:15+00:00Added an answer on May 13, 2026 at 7:57 pm

    You should use the member_roles association instead of roles association.

      def update
        # The role hash should contain :role_id, :scope and if set, :sport_id
        roles = ((params[:member] ||= {}).delete "roles") || []
        MemberRole.transaction do 
    
          # Next line will associate the :sport_id and :scope to member_roles
          # EDIT: Changed the code to flush old roles.
          @member.member_roles = roles.collect{|r| MemberRole.new(r)}
    
          # Next line will save the member attributes and member_roles in one TX
          if @member.update_attributes params[:member]
            # some code
          else
            # some code
          end
        end
      end
    

    Make sure you enclose everything in one transaction.

    Other possibility is to use accepts_nested_attributes_for on member_roles association.

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

Sidebar

Related Questions

Taking the typical products / categories many-to-many relationship you'd typically have a relationship like
So I have typical 1:M relationship: Car can have many Models I want to
I have a typical Parent contains many Children (List) relationship. How do I tell
Consider this example (typical in OOP books): I have an Animal class, where each
I have a many to many relationship table whith some datas in the jointing
I have a typical HABTM relationship between members (actual members of our organization, not
I am setting up a simple has_many through relationship. I was wondering if there
using c# vs2008 winforms. My standard forms have a typical usage pattern of Form
In a typical MVC application we have validation that occurs in many different places.
We have a typical business application with an Outlook-looking Winforms client talking to asmx

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.