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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:31:00+00:00 2026-05-30T09:31:00+00:00

A user has many user_levels and a user_level belongs to a user. Here is

  • 0

A user has many user_levels and a user_level belongs to a user. Here is the code for user_level for update:

<%= simple_form_for @user do |f| %>
  <% @user.user_levels.each do |level| %>    
    <%= f.fields_for :user_levels, level do |builder| %>
      <p><%= render :partial => 'user_levels', :locals => {:f => builder, :i_id => level.position} %></p>  
    <% end %> 
  <% end %>
<% end %>

The code above will display the current user_level with selected set to the current position. The problem is that the update was not saved for user_level.

Here is the user_levels partial

<div class="fields">
  <%= f.input :position, :collection => return_position, :prompt => "Choose position", 
                      :label => false, :include_blank => true, :selected => i_id %>
  <%= link_to_remove_fields "remove", f %>
</div>

Here is the string posted to the server for params[:user]. There are currently two positions for the user and one gets deleted as update:

{"name"=>"test eng", "login"=>"tester12", "password"=>"password", "password_confirmation"=>"password", "user_type"=>"employee", "user_levels_attributes"=>{"0"=>{"id"=>"5"}, "1"=>{"position"=>"elec_eng", "_destroy"=>"false", "id"=>"6"}}}

Any solution for child update? Thanks so much

UPDATE:

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    @user.input_by_id = session[:user_id]
    if @user.save!
      redirect_to URI.escape("/view_handler?index=0&msg=saved!")
    else
      flash.now[:error] = 'not saved!'
      render 'new'
    end
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    @user.input_by_id = session[:user_id]
    if @user.update_attributes(params[:user])
      redirect_to URI.escape("/view_handler?index=0&msg=saved}")
    else
      flash.now[:error] = 'not saved!'
      render 'edit'
    end
  end

user.rb:

  has_many :user_levels, :dependent => :destroy
  accepts_nested_attributes_for :user_levels, :reject_if => lambda { |a| a[:position].blank? }, :allow_destroy => true
  validates_associated :user_levels
  • 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-30T09:31:02+00:00Added an answer on May 30, 2026 at 9:31 am

    Try removing the line @user.user_levels.each do |level| and please post what issue pops up then.
    And does the user model accept nested attributes for user levels ?

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

Sidebar

Related Questions

I have a simple databasescheme: User, Account. User has 1-to-many relationship with Account. I
Suppose user has opened my web application in many different browser windows. After sometime
I'm currently working on a program that has many of those the user SHOULD
I'm a long-time user of the DevExpress XPO library. It has many great features,
I have many to one relation, let's say User has a Company. Company entity
I have a many-to-many relationship like this: A user has_many organizations through affiliations and
I have a sharepoint webpart that will exist on many pages. The webpart has
Using Doctrine PHP If I have a user with a many to many relationship
I have a function like : # get all locations, if the user has
I know this kind of question has been asked before, for example here: Android:

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.