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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:39:56+00:00 2026-05-30T06:39:56+00:00

I have a Users model that can have many Groups through Memberships. If a

  • 0

I have a Users model that can have many Groups through Memberships. If a user is a member of a group, and you submit the form with no changes, it will attempt to update the membership table and put group_id to 0 as per below.

My model for User accepts_nested_attributes_for Memberships

In my User controller…

    def edit
      @user = User.find(params[:id])
      @groups = Group.current
      @membership = @user.memberships.build
    end

    def update
      @user = User.find(params[:id])
      @groups = Group.current

      if params[:memberships][:group_id] != ""
        @membership = @user.memberships.build(:group_id => params[:memberships][:group_id])
      end

      respond_to do |format|
        if @user.update_attributes(params[:user])
          format.html { redirect_to admin_users_url, notice: 'User was successfully updated.' }
          format.json { head :no_content }
        else
          #if params[:memberships][:group_id] == ""
          #  @membership = @user.memberships.build
          #end
          format.html { render action: "edit" }
          format.json { render json: @user.errors, status: :unprocessable_entity }
        end
      end
    end

In my user form…

<%= form_for([:admin,@user]) do |f| %>
---
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation %>
  </div>
    <%= f.fields_for :memberships do |builder| %>
  <div class="field">
    <% if builder.object.new_record? %>
        <%= builder.label :group_id %><br />
        <%= collection_select(:memberships, :group_id, @groups, 'id', 'name',    {:include_blank => true}) %>
    <% else %>
        <%= builder.label :group_id %><br />
        <%= builder.text_field :group_id, :value =>    Group.find(Membership.find(builder.object).group_id).name, :readonly => true %> <%= link_to 'Remove', [:admin,@user,Membership.find(builder.object)], :confirm => 'Are you sure?', :method => :delete %>
    <% end %>
  </div>
    <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

The form is submitting the data

Parameters: {"utf8"=>"✓", 
"authenticity_token"=>"6Vp9sgMySzRm2CU9Dko+Jpf6yaBkXjKkt10UDbb8dcw=", 
"user"=>{"email"=>"asdfasdf@asdf.com", 
"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", 
"memberships_attributes"=>{"0"=>{"group_id"=>"Cricket Maidstone June 2012",
"id"=>"18"}}}, 
"memberships"=>{"group_id"=>""}, 
"commit"=>"Update User", "id"=>"25"}

All I want to do is for the form to check and if there is nothing in the collection_select field to not update the memberships table, but still update any changes to email/password. Can anyone see a way to do this?

  • 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-30T06:39:58+00:00Added an answer on May 30, 2026 at 6:39 am

    In your model, declare a parallel field with attr_accessor:

    attr_accessor :memberships_attributes_input
    
    def memberships_attributes_input
        memberships
    end
    
    def memberships_attributes_input=value
        # Update memeberships if required evaluating 'value'.
    end
    

    Use this new field in your form instead “memberships”.

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

Sidebar

Related Questions

I have an Organization model that has_many users through affiliations. And, in the form
I have two tables, users and groups . A user can belong to many
I have a event model that has many invitations. Invitations are setup through checkboxes
I have a form I'm trying to set up ... Users can have many
Say that I have two models- Users and Accounts. Each account can have at
I have a model called company that has_many users then users belongs_to company. class
I have a user model that requires the user to change their password every
So I have a User model that :has_many other models like Documents, Videos, Posts
I have a model that has a ForeignKey to the built-in user model in
I have a user model on which I check to make sure that the

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.