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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:02:31+00:00 2026-05-27T11:02:31+00:00

In my code below I want to assign the current user to the products

  • 0

In my code below I want to assign the current user to the products that are created.

ProductsController.rb

def new
  @products = Array.new(2) { Product.new }   
end

def create_multiple
  params[:product][:user_id] = current_user.id
  Product.transaction do
    begin
      @products = Product.create!(params[:products].map {|_k, up| up.merge params[:product]})
      redirect_to :back, :notice => "Success!"
    rescue ActiveRecord::Rollback
      redirect_to :back, :notice => "An error occured, please try again."
    end
  end
end

new.html.erb

<%= form_tag create_multiple_products_path, :method => :post do %>

  <%= date_select("product", "purchase_date")  %>

  <% @products.each_with_index do |product, index| %>
     <%= fields_for "products[#{index}]", product do |up| %>
        <%= render "fields", :f => up %>
     <% end %>
  <% end %>

  <%= submit_tag "Done" %>
<% end %>

The USER ID (current user) isn’t being assigned. Any ideas on how to assign it?

  • 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-27T11:02:32+00:00Added an answer on May 27, 2026 at 11:02 am

    You can eliminate a lot of logic here by remembering two facts: create will take an array of Hashes to create multiple objects, and its bang-method counterpart create! will throw an exception if any validations failed.

    def create_multiple
      # like clyfe said, do this in a transaction
      Product.transaction do
        begin
          @products = current_user.products.create!(
            # Hash#map returns an Array, so no need to use Hash#values
            params[:products].map { |_k, up| up.merge params[:product] }
          )
    
          # if create! is successful...
          redirect_to :back, :notice => "Success!"
    
        # if it's not successful it'll throw this exception, which we can rescue
        rescue ActiveRecord::Rollback
          redirect_to :back, :notice => "An error occured, please try again."
        end
      end
    end
    

    One way to get deal with the create! issue is to just get rid of any “blank” entries in a before_filter, e.g.:

    before_filter :toss_blank_products
    
    def toss_blank_products
      params[:products].reject! do |prod|
        # this logic may need to be more complex depending on the meaning of "blank"
        # for your model/form
        prod.values.all? &:blank
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to assign a value to a checkbox. The code below doesn't seem
How to assign PHP array values to JavaScript array? Using below PHP code I
See code below: I want Page 1 to alert #testing... This works in C
I have the code below and I want to loop this every 5 seconds.
As a PHP developer, I always use the code below whenever I want to
The code below does not work but meant to illustrate what I want to
I want to know if the code below removes all input type='text' values back
Using the code below, I want to keep the same menu div opened in
In the code below, I don't want to use instanceof to know the type
I have the following code below which works, but I want to insert values

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.