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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:26:44+00:00 2026-06-18T04:26:44+00:00

I have followed railscast #198 Edit Multiple Individually and it works 100% for editing

  • 0

I have followed railscast #198 Edit Multiple Individually and it works 100% for editing multiple records from a single form.

However, I would like to also create new records from the same form if the record. i.e. in the controller assess the hash of params from the form, update those records that exist and create the records that don’t exist.

Also, if any validation errors occur they should be caught and passed back.

This in the controller works and will update correctly if the fields for the new record aren’t present
@costs = IngredientCost.update(params[:ingredient_costs].keys, params[:ingredient_costs].values).reject { |item| item.errors.empty? }

For the fields for the new records I am currently creating an artibrary large unique number as the record id (I’m not sure if this is right to do) and then the message comes back that it cannot be saved because the id doesn’t exist, which is to be expected as I it doesn’t exist.

I’ve tried the following two snippets as a stab in the dark but not really sure which way I should be going..

@costs = IngredientCost.find_or_initialize_by_id(:id => params[:ingredient_costs].keys).update(params[:ingredient_costs].keys, params[:ingredient_costs].values).reject { |i| i.errors.empty? }

and

params[:ingredient_costs].each do |ingredient_cost|
  @cost = IngredientCost.find_or_initialize_by_ingredient_id_and_user_id(:ingredient_id => ingredient_cost[1]["ingredient_id"], :user_id => current_user.id)
  @cost = IngredientCost.update(:ingredient_id => params[:ingredient_costs][ingredient_cost[0]]["ingredient_id"], :quantity => params[:ingredient_costs][ingredient_cost[0]]["quantity"], :price_per_unit => params[:ingredient_costs][ingredient_cost[0]]["price_per_unit"], :weight_id => params[:ingredient_costs][ingredient_cost[0]]["weight_id"])

Neither seem to work.

I’ve seen this work before but for updating/creating nested attributes from one form. I don’t really want to have to go that route with 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-06-18T04:26:46+00:00Added an answer on June 18, 2026 at 4:26 am

    I eventually resorted to saving the multiple records through a parent model which already had the correct associations.

    I followed Railscast #196 Nested Model Forms which was fairly similar to above only saving to another model and placing accepts_nested_attributes_for in that model’s *.rb.

    Remember to move the routes.

    Move the controller code to the receiving action of the parent model.

    My form in the viewer now looks like this (in haml format):

    = form_for @user, :remote => true do |f|
      = f.fields_for :ingredient_costs do |builder|
        // fields inserted here
    

    It took me a couple hours to realise I had to use builder.object... to get to the child that I actually am interested. The .object class wasn’t obvious to me.

    Anyway, hope this was useful to someone. I think SybariteManoj’s answer looks like a good deal, but I couldn’t get it to work.

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

Sidebar

Related Questions

I have followed a screen cast form ryan bates railscast and every thing is
I have followed the onTouch example from google located here . However, I get
I followed RailsCasts authentication from scratch (http://railscasts.com/episodes/250-authentication-from-scratch-revised) using the bcrypt-ruby gem and have the
I have followed the following railscast about adding pdfkit to an application, and I
I have followed Railscast episode 250 revised to create user authentication with has_secure_password which
I've just followed the Railscast tutorial: http://railscasts.com/episodes/262-trees-with-ancestry Is it possible to paginate results from
Have followed every step correctly from this link http://msdn.microsoft.com/en-us/library/ms746621.aspx When trying to localize the
I followed a railcast to search a single column from a database, which worked
I'm following railscast and have followed the instructions exactly. I've even used net.tutsplus's faye.ru
I've read that others have followed the Endless Page railscast and modified it to

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.