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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:06:38+00:00 2026-06-05T21:06:38+00:00

In our rails 3.1.4 app, there are user (parent) and user_levels (child) models. class

  • 0

In our rails 3.1.4 app, there are user (parent) and user_levels (child) models.

class User < ActiveRecord::Base
  attr_accessible :name, :login, :password, :user_levels_attributes, :as => :role_new
  has_many :user_levels
  ccepts_nested_attributes_for :user_levels, :allow_destroy => true, :reject_if => proc { |a| a['position'].blank? }
  validates_associated :user_levels
end

class UserLevel < ActiveRecord::Base
  belongs_to :user
  validates_presence_of :position
end

The position column in user_level has to be filled. If it is not, both user and user_levels should NOT be saved. The problem with the above is that it always causes error of “position can’t be blank” even there is a position value in the params.

{"utf8"=>"✓",
 "user"=>{"name"=>"tester ceo",
 "login"=>"testceo",
 "update_password_checkbox"=>"[FILTERED]",
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]",
 "user_levels_attributes"=>{"1339886115748"=>{"position"=>"ceo"}}},
 "commit"=>"Save"}

After deleting user_levels_attribues, the model saves user into users table no matter what kind of user_levels is filled int. We did extensive online search and haven’t found a solution yet. For example, if validates_presence_of :user_levels is added to the user model, then none of the user levels can be saved or updated, even with a valid position.

Any suggestion about how to implement the orchestrated saving/updating of two associated models? Thanks so much.

UPDATE:
_user_level.html.erb:

<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>

_form_new.html.erb:

<%= simple_form_for @user do |f| %>
  <%= f.input :name, :label => 'name:' %>
  <%= f.input :login, :label => 'login:', :hint => '6 digits ' %>
  <%= f.input :password, :label => 'password:', :hint => '6digits', :input_html => {:id => 'new_user_password'} %>
  <%= f.input :password_confirmation, :label => 'confirmation:' %>  
  <%= f.input :user_type, :label => 'user type:', :collection => return_user_type, :include_blank => true %>
  &nbsp;&nbsp;position:
  <p><%= link_to_add_fields "Choose position", f, :user_levels %></p>

  <p><%= f.button :submit, 'Save' %></p>
<% end %>

Here is the method to add position field:

  #add user position in system user creation
  def link_to_add_fields(name, f, association)
    new_object = f.object.class.reflect_on_association(association).klass.new
    fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
      render :partial => association.to_s, :locals => {:f => builder, :i_id => 0} 
    end
    link_to_function(name, "add_fields(this, \"#{association}\", \"#{j fields}\")")
  end

JS file:

function add_fields(link, association, content) {
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + association, "g")
  $(link).parent().before(content.replace(regexp, new_id));
}
  • 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-05T21:06:40+00:00Added an answer on June 5, 2026 at 9:06 pm

    I think you need to remove the :reject_if and change validates_associated :user_levels to validates_presence_of :user_levels.

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

Sidebar

Related Questions

There is a purchasing controller in our rails 3.1.0 app. A purchasing needs 4
There are rfqs and quotes controllers in our rails 3.1.0 app. Rfq has many
There is a mass assignment defined in sys_log model in our rails 3.1.4 app:
There is a before_filter in our rails 3.1 app check the session time out.
We are getting close to releasing our new rails app and so far interest
I'm looking into database backup options for our Heroku rails app, and it looks
We just upgraded our app to Rails 3.2.2 and are now having a routing
I wanted to add user search auto-complete (like Facebook's) to my Rails app on
There are some local files (that are part of our repository) in a rails
I've been working on getting our Rails 2.3.8 app running correctly under JRuby. Everything

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.