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?