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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:52:39+00:00 2026-06-14T00:52:39+00:00

After a number of hours reading stackoverflow and watching railscasts, I’ve decided to post.

  • 0

After a number of hours reading stackoverflow and watching railscasts, I’ve decided to post. The question is very similar if not identical to many other questions here but I’m just not getting it.
This is my first has_many_through association.

class User < ActiveRecord::Base
...
has_many :affiliations
has_many :sublocations, through: :affiliations
...
end

class Sublocation < ActiveRecord::Base
...
has_many :affiliations
has_many :users, through: :affiliations
...
end

class Affiliations < ActiveRecord::Base
...
belongs_to :user
belongs_to :sublocation
...
end

The affiliations table has the usual user_id and sublocation_id columns. It also has boolean column named ‘default’.
In my ‘new/edit user’ form(s), I’m needing to select one or more sublocations via checkboxes and also include a way to mark a sublocation as ‘default’.

Again, I’ve read example after example but something just isn’t ‘clicking’ in my brain. I’m not necessarily looking for an exact solution but a nudge in the right direction.

Many thanks,
CRS

  • 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-14T00:52:40+00:00Added an answer on June 14, 2026 at 12:52 am

    My suggestion:

    Create a user form to set up the associations between User and Sublocation

    I am assuming the sublocations have been pre-populated?

    users_controller.rb

    def new
        @user = User.new
        @sublocations = Sublocation.all
        respond_to do |format|
          # what ever
        end
    end
    
    def create
        @user = User.new(params[:user])
        params[:sublocation_ids].each do |key, value|
          @user.affiliations.build(sublocation_id: value)
        end
        respond_to do |format|
          if @user.save
            # what ever
          else
            # what ever
          end
        end
     end
    
    def show
        @user = User.find(params[:id])
        @affiliations = @user.affiliations
    end
    
    def set_default
        affiliation = Affiliation.find(params[:affiliation_id])
        Affiliation.where(user_id: params[:user_id]).update_all(default: false)
        affiliation.toggle!(:default)
        redirect_to affiliation.user, notice: "Default sublocation set"
     end
    

    users/_form.html.haml

    = form_for @user do |f|
    
      .field
        # fields for user attributes
    
      .field
    
        %h1 Sublocations
    
        = f.fields_for "sublocations[]" do
          - for sublocation in @sublocations
            = label_tag sublocation.name
            = check_box_tag "sublocation_ids[#{sublocation.name}]", sublocation.id, @user.sublocations.include?(sublocation)
      .actions
        = f.submit 'Save'
    

    Then perhaps on the user show page, list all their sublocations and create a form for them to set a default.

    users/show.html.haml

    = form_tag user_set_default_path(@user), method: :put do
      %table
        %tr
          %th Sublocation
          %th Default?
        - @affiliations.each do |affiliation|
          %tr
            %td= affiliation.sublocation.name
            %td= radio_button_tag :affiliation_id, affiliation.id, affiliation.default? ? {checked: true} : nil
    
      = submit_tag "Set default"
    

    routes.rb

    resources :users do
      member do
       put "set_default"
      end
    end 
    

    Hope this helps or at least gets you on the right path.

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

Sidebar

Related Questions

I finally decided myself to post my problem, after a couple of hours spent
If the question doesn't seem very clear, it should be after this explanation !
After several hours, I'm at my wit's end, even after reading reams of documentation
After running for a number of hours on Linux, my Python 2.6 program that
I have a question to ask about notifications. After some hours to learn how
After a number of failed deployment attempts (configs overwritten, files missing, out dated etc)
Extract the number after #/ from my string: abcdefg#/123 .
I need to get the number after the button to increment in a for
i want to truncate a number after decimal through a built in function in
The below textbox is printing the value of the phone number after getting it

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.