# What is wrong in this method?

**URL:** <https://www.ruby-forum.com/t/what-is-wrong-in-this-method/85208>\
**Category:** Rails\
**Created:** [March 22, 2006, 10:15am UTC](https://www.ruby-forum.com/t/what-is-wrong-in-this-method/85208 "2006-03-22T10:15:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [March 22, 2006, 10:15am UTC](https://www.ruby-forum.com/t/what-is-wrong-in-this-method/85208/1 "2006-03-22T10:15:14Z")

</div>

Hi,

# This is my definition of method “add\_to\_cart”

def add\_to\_cart  
count=params[:product][:count]  
while count.to\_i\>=0.to\_i  
if params[:product][‘dem\_quantity’+count.to\_s]==""

```
  else
    @product = 

```

# Product.find(params[:product][‘product\_id’+count.to\_s]) print @product.name @product.update\_attributes(params[:product][‘dem\_quantity’+count.to\_s]) end count=count.to\_i-1 end end

# With this method I am trying to just update values of the field “demand” in the database corresponding to particular “id”. But when I m executing this I m getting error like:-

# undefined method `stringify\_keys!’ for “5”:String

# Application Trace of error is:-

# C:/local/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/base.rb:1333:in `attributes=' C:/local/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1266:in`update\_attributes’ #{RAILS\_ROOT}/app/controllers/products\_controller.rb:15:in `add\_to\_cart’ -e:3

What is wrong in above method?

Please tell me.  
Thanx in advance.  
Prash

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [March 22, 2006, 11:17am UTC](https://www.ruby-forum.com/t/what-is-wrong-in-this-method/85208/2 "2006-03-22T11:17:11Z")

</div>

Am Mittwoch, den 22.03.2006, 10:15 +0100 schrieb Prashant T.:

> ```
> @product = 
> 
> ```
> 
> # in the database corresponding to particular “id”. But when I m executing `update_attributes' #{RAILS_ROOT}/app/controllers/products_controller.rb:15:in `add\_to\_cart’ -e:3
> 
> What is wrong in above method?

The error tells you, that you have a String where a Hash was expected.  
Make sure params[:product] contains a Hash.

–  
Norman T.

[http://blog.inlet-media.de](http://blog.inlet-media.de)

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [March 22, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/what-is-wrong-in-this-method/85208/3 "2006-03-22T16:45:08Z")

</div>

Prashant T. wrote:

> ```
> @product.update_attributes(params[:product]['dem_quantity'+count.to_s])
> end
> count=count.to_i-1
> end
> 
> ```
> 
> # end
> 
> What is wrong in above method?

I see you assign count as a string from the params, then start a while  
loop using that string.

Then you do “count.to\_s” which attempts to stringify a string.

That, or the count.to\_i after you assign it to a numeric object.

Chris
