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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:26:41+00:00 2026-06-15T23:26:41+00:00

There is a situation where I need to call my update action from my

  • 0

There is a situation where I need to call my update action from my create action:

def create
  @product = Product.find_or_initialize_by_sku(params[:sku])
  if @product.new_record?
    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: 'Product was successfully created.' }
        format.json { render json: @product, status: :created, location: @product }
      else
        format.html { render action: "new" }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  else
   update ##*** I am calling the update action at this point ***##
  end
end

def update
  @product = Product.find_or_initialize_by_sku(params[:sku])

  respond_to do |format|
    if @product.update_attributes(params[:product])
      format.html { redirect_to @product, notice: 'Product was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @product.errors, status: :unprocessable_entity }
    end
  end
end

But I end up making two identical queries to my database because I am not sure of the most effective way to pass a variable (in this case, the instance variable @product) from one action to another.

I am trying to get a grasp on the ‘rails’ way to do this. What should I do here (as I know I should not be making two identical database queries)?

Would I store it in the session as if I was performing a redirect (Am I performing a redirect when I call an action from within another action like this?)?

session[:product] = @product # in my create action
# then
@product = session[:product] # in my update action

Does caching make this all a moot point?

  • 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-15T23:26:42+00:00Added an answer on June 15, 2026 at 11:26 pm

    Consider using memoization:

    @product ||= Product.find_or_initialize_by_sku(params[:sku])
    

    The ||= operator will check if @product is nil, and if it is, Ruby will execute the finder. Otherwise, Ruby will use the current value. Therefore, when you call update from create, the update method will use the @product that you found or initialized in create.

    Regarding your second question, you’re not performing a redirect when you call one controller action from another. Try not to over-think it – a controller is just another Ruby class with its own instance variables (e.g. @product) and methods. To perform a redirect, you would have to call redirect_to, which would result in a second web request to your controller.

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

Sidebar

Related Questions

There is a situation where I need to have a suspend kind of behavior
is there any way how in this situation insert and update DB with single
I have a situation where I need to do an update on a very
There's a special situation in my app where I need a UIButton touch to
Situation: There are several entities in a simulated environment, which has an artificial notion
I have this situation: There are a login page with a login form (form
I have the following situation: There is one custom view inside of the first
I will demonstrate the problem on a real example so the situation: there is
There is the situation - i made some math modelling app on C#(WPF), showing
Suppose there is a situation like : statement 1; statement 2; statement 3; statement

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.