Creating an array of hash values on update method

Hi all,

I’m trying to do a small shopping cart and I’ve come across a problem I
believe some of you may consider simple.

I’m basically using two models to do the shopping cart: Orders and
Order_Items

A shopping cart is basically an order (with a state of incomplete) with
each product being an order_item attached to the order. So my shopping
cart page shows the multiple order_items for this particular order - the
cart page itself is an action and view in the Order controller/model.

My problem comes when I try to edit the quantities in the shopping cart.
The only way I’ve gotten close to making changes is by calling the
OrderItem.update() call… see code below:

Orders_Controller.rb

def cart
@order = current_order
end

Order_Items_Controller.rb

def quantity_update
#I would need to pass the params[:quantity] fields instead of
hardcoding the quantities
qtys = {“quantity” => 2}, {“quantity” => 5}, {“quantity” => 3}
OrderItem.update(params[:ids], qtys) #params[:ids] has an array of 3
order_item id’s in this case
end

Cart.html.erb

<% form_tag quantity_update_order_items_path, :method => :put do %>

<% for order_item in @order.order_items %> <%= hidden_field_tag 'ids[]', order_item.id %> <% end %>
Product Qty Unit Price Full Price
<%=h order_item.product.name %> <%= text_field_tag 'quantity[]', order_item.quantity %> <%#= order_item.quantity %> <%= number_to_currency(order_item.unit_price) %> <%= number_to_currency(order_item.full_price) %> <%= link_to 'Remove', order_item, :confirm => 'Are you sure?', :method => :delete %>
Total: <%= number_to_currency @order.total_price %>

<%= submit_tag “Update” %>

<% end %>

By the way, the params[:ids] is a hidden field in the cart view.

Basically what I need to do is find a way to create an array of hashes
where the key is the quantity field and the value is in the
params[:quantity] for the appropriate id. The above code works although
it is obviously hard coded.

I was thinking something like this may work:

for i in params[:quantity]
qtys = {‘quantity’ => i}
end

But it doesn’t…

Also - I can’t help to feel this is bad approach. There must be a
cleaner way to update the multiple quantity records without having to do
this, right?

Any input would be GREATLY appreciated! Let me know if something is
unclear or if you have any questions! Thanks!

-Tony

Tony T. wrote:

Hi all,

I’m trying to do a small shopping cart and I’ve come across a problem I
believe some of you may consider simple.

I’m basically using two models to do the shopping cart: Orders and
Order_Items

A shopping cart is basically an order (with a state of incomplete) with
each product being an order_item attached to the order. So my shopping
cart page shows the multiple order_items for this particular order - the
cart page itself is an action and view in the Order controller/model.

My problem comes when I try to edit the quantities in the shopping cart.
The only way I’ve gotten close to making changes is by calling the
OrderItem.update() call… see code below:
[…]

I think you’re going about this entirely the wrong way. Please take a
look at Stephen Chu’s params[:fu] series, and perhaps the Railscast on
complex forms, then start over.

BTW, you should remove the capital letters from your filenames.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I think you’re going about this entirely the wrong way. Please take a
look at Stephen Chu’s params[:fu] series, and perhaps the Railscast on
complex forms, then start over.

BTW, you should remove the capital letters from your filenames.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen,

Thank you for the reply! I’m pretty sure you’re correct in saying this
isn’t the right way to do things…

However, I found a solution to my problem - which I think should work
fine in my scenario. However I’d love to know if anyone can find a
cleaner solution to the below script:

def quantity_update
i=0
until i == params[:ids].size
OrderItem.update(params[:ids][i], {“quantity” =>
params[:quantity][i]})
i += 1
end

redirect_to #wherever you want…
end

Any thoughts or opinions would be appreciated!

-Tony

My biggest concern comes in the form of validations.

To confirm that an item is not 0 or negative and to also confirm is in
fact a number and not a character(s).

-Tony

Nevermind…

validates_numericality_of :quantity, :greater_than => 0

took care of that.

Hope this helps someone!

Tony T. wrote:

I think you’re going about this entirely the wrong way. Please take a
look at Stephen Chu’s params[:fu] series, and perhaps the Railscast on
complex forms, then start over.

BTW, you should remove the capital letters from your filenames.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen,

Thank you for the reply! I’m pretty sure you’re correct in saying this
isn’t the right way to do things…

Then don’t do it that way.

However, I found a solution to my problem - which I think should work
fine in my scenario. However I’d love to know if anyone can find a
cleaner solution to the below script:

def quantity_update
i=0
until i == params[:ids].size
OrderItem.update(params[:ids][i], {“quantity” =>
params[:quantity][i]})
i += 1
end

redirect_to #wherever you want…
end

Any thoughts or opinions would be appreciated!

You’re still sticking to your old wrong way of doing things – or am I
misunderstanding? Take the time to read those references I suggested
and do it right!

-Tony

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Hi Marnen,

I read and have gone through the complex forms railscasts but they
didn’t quite apply to my multi records with same IDs and different value
needs. I haven’t had a chance to view the params series - I will by this
weekend though. Thanks for pointing those sources out by the way.

I DO believe there is a better way to approach this (mainly more
efficient) - that doesn’t mean my solution is 100% wrong. Like I
mentioned, it’s working flawlessly at the moment.

I would like to know why you think my solution is wrong though. I don’t
do this for a profession and I’m just learning mostly as a hobby. Design
is my forte. I will read the params series and hopefully pick up a
better approach.

Thanks again for the (constructive) criticism.

-Tony

Tony T. wrote:

Hi Marnen,

I read and have gone through the complex forms railscasts but they
didn’t quite apply to my multi records with same IDs and different value
needs. I haven’t had a chance to view the params series - I will by this
weekend though. Thanks for pointing those sources out by the way.

I DO believe there is a better way to approach this (mainly more
efficient) - that doesn’t mean my solution is 100% wrong. Like I
mentioned, it’s working flawlessly at the moment.

I would like to know why you think my solution is wrong though.

Because you’re trying to extract and update one field from several
records, rather than working with the object graph as a whole. In doing
so, you are working too hard and poorly reinventing what Rails will
already do for you if you let it.

I don’t
do this for a profession and I’m just learning mostly as a hobby.

Understood. I do do this professionally (though not exclusively), but
I’m 98% self-taught. That may be why I care so much about good
development practice…

Design
is my forte. I will read the params series and hopefully pick up a
better approach.

Do. Just remember that Rails will make your life easy if you trust it.

Thanks again for the (constructive) criticism.

-Tony

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]