# Creating an array of hash values on update method

**URL:** <https://www.ruby-forum.com/t/creating-an-array-of-hash-values-on-update-method/179716>\
**Category:** Rails\
**Created:** [December 2, 2009, 3:24pm UTC](https://www.ruby-forum.com/t/creating-an-array-of-hash-values-on-update-method/179716 "2009-12-02T15:24:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [December 2, 2009, 3:24pm UTC](https://www.ruby-forum.com/t/creating-an-array-of-hash-values-on-update-method/179716/1 "2009-12-02T15:24:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [December 2, 2009, 3:48pm UTC](https://www.ruby-forum.com/t/creating-an-array-of-hash-values-on-update-method/179716/2 "2009-12-02T15:48:02Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [December 3, 2009, 1:16am UTC](https://www.ruby-forum.com/t/creating-an-array-of-hash-values-on-update-method/179716/3 "2009-12-03T01:16:16Z")

</div>

> 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](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

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

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [December 3, 2009, 1:30am UTC](https://www.ruby-forum.com/t/creating-an-array-of-hash-values-on-update-method/179716/4 "2009-12-03T01:30:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [December 3, 2009, 1:33am UTC](https://www.ruby-forum.com/t/creating-an-array-of-hash-values-on-update-method/179716/5 "2009-12-03T01:33:41Z")

</div>

Nevermind…

validates\_numericality\_of :quantity, :greater\_than =\> 0

took care of that.

Hope this helps someone!

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [December 3, 2009, 5:22am UTC](https://www.ruby-forum.com/t/creating-an-array-of-hash-values-on-update-method/179716/6 "2009-12-03T05:22:02Z")

</div>

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](http://www.marnen.org)  
> > removed\_email\_address@domain.invalid
> 
> 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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [December 3, 2009, 5:40am UTC](https://www.ruby-forum.com/t/creating-an-array-of-hash-values-on-update-method/179716/7 "2009-12-03T05:40:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [December 3, 2009, 7:16am UTC](https://www.ruby-forum.com/t/creating-an-array-of-hash-values-on-update-method/179716/8 "2009-12-03T07:16:10Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid
