# Rails - depot application

**URL:** <https://www.ruby-forum.com/t/rails-depot-application/113417>\
**Category:** Rails\
**Created:** [August 30, 2007, 10:32pm UTC](https://www.ruby-forum.com/t/rails-depot-application/113417 "2007-08-30T22:32:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![david.harris.uk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@david.harris.uk](https://www.ruby-forum.com/u/david.harris.uk)\
**Post date:** [August 30, 2007, 10:32pm UTC](https://www.ruby-forum.com/t/rails-depot-application/113417/1 "2007-08-30T22:32:51Z")

</div>

I’m doing the depot example in the Agle Web development book. I’m  
getting this error:

"undefined method `product’ for #BigDecimal:331ce70,‘0.21E1’,8(8) "

"Extracted source (around line #9):

8: for item in @items  
9: product = item.product  
10: -%\>  
11:

  
12: \<%= item.product%\>"  
this is display\_cart.rhtml

line\_item.rb has:

"class LineItem \< ActiveRecord::Base  
belongs\_to :product

def self.for\_product(product)  
item = self.new  
item.quantity = 1  
item.product = product  
item.unit\_price = product.price  
end  
end "

cart.rb has:

"class Cart

attr\_reader :items  
attr\_reader :total\_price

def initialize  
@items = []  
@total\_price = 0.0  
end

def add\_product(product)  
@items \<\< LineItem.for\_product(product)  
@total\_price += product.price  
end  
end "

It looks like product = item.product in display\_cart.rhtml is pulling  
out a decimal number not a content array item. But I have no great  
idea why or how to find out what is being put into @items in the cart  
class.

I haven’t been able to find a Google solution to this and I dont  
_think_ I have any typos (the downloaded sample files from the Agiles  
site at pragmaticprogrammer produces the same error)

---

<div class="post-metadata">

**Author:** ![david.harris.uk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@david.harris.uk](https://www.ruby-forum.com/u/david.harris.uk)\
**Post date:** [August 31, 2007, 11:28am UTC](https://www.ruby-forum.com/t/rails-depot-application/113417/2 "2007-08-31T11:28:57Z")

</div>

On Aug 30, 9:31 pm, “[david.harris.uk](http://david.harris.uk)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 11:   
> item.quantity = 1  
> attr\_reader :total\_price  
> end "
> 
> It looks like product = item.product in display\_cart.rhtml is pulling  
> out a decimal number not a content array item. But I have no great  
> idea why or how to find out what is being put into @items in the cart  
> class.
> 
> I haven’t been able to find a Google solution to this and I dont  
> _think_ I have any typos (the downloaded sample files from the Agiles  
> site at pragmaticprogrammer produces the same error)

Hi David,

Here’s my guess:

“undefined method `product’ for #BigDecimal:331ce70,‘0.21E1’,8(8)”  
means that Ruby’s trying to call the method product on a BigDecimal.  
On line 9 (where the error orginates), product is being called on the  
variable item. Item is an element of the @items array. @items is being  
built in the add\_product method of the Cart class, by calling  
for\_product(product) on LineItem. And this is where I think the  
problem is: the last line of the for\_product(product) method assigns a  
price to the item variable.

Every expression in Ruby returns a value. The value returned from  
performing an assignment is what the right-hand side of the expression  
evaluates to. Furthermore, the last expression in a Ruby method is the  
return value of that method. So, instead of returning a LineItem from  
the for\_product(product) method, this code is actually returning the  
value of the price variable, a BigDecimal.

In short, I believe the following code will fix your problem:

def self.for\_product(product)  
item = self.new  
item.quantity = 1  
item.product = product  
item.unit\_price = product.price  
item  
end

If you find this idiom confusing (I know I do), you may use the return  
keyword (although hard-core Rubyists may frown upon it!):

def self.for\_product(product)  
item = self.new  
item.quantity = 1  
item.product = product  
item.unit\_price = product.price  
return item  
end

Cheers,  
Louis.

---

<div class="post-metadata">

**Author:** ![david.harris.uk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@david.harris.uk](https://www.ruby-forum.com/u/david.harris.uk)\
**Post date:** [September 6, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/rails-depot-application/113417/3 "2007-09-06T20:56:01Z")

</div>

I’m still getting "undefined method `product’ for #\<BigDecimal:  
331ce70,‘0.21E1’,8(8)\> "

So this promising idea didnt work. I altered display\_cart.rhtml thus:

"

\<%  
for item in @items  
@dbg = item.type  
@v1=item.methods  
@v2= item.instance\_variables  
-%\>

"

which gives:  
"  
wibble: LineItem =  
instance variables: @attributes@new\_record@product \*  
methods: before\_createcreate\_or\_updatesave\_with\_validation[stuff]  
"

So item is a LineItem class not a decimal and this looks like an  
ActiveRecord class.

Which makes me wonder if this application ever worked: it’s not a typo  
by me I think since I downloaded the source from the website and get  
the same error, but I seem to be the only one having this problem. So  
I don;t know. I think I’ll dump the example from the book since I seem  
to be spending more time debugging it than learning Rails!

|
| |

---

<div class="post-metadata">

**Author:** ![david.harris.uk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@david.harris.uk](https://www.ruby-forum.com/u/david.harris.uk)\
**Post date:** [September 7, 2007, 10:52am UTC](https://www.ruby-forum.com/t/rails-depot-application/113417/4 "2007-09-07T10:52:18Z")

</div>

On Sep 6, 7:54 pm, “[david.harris.uk](http://david.harris.uk)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> @v1=item.methods
> 
> ```
> 
> > > 8: for item in @items
> 
> > > "class Cart  
> > > @items \<\< LineItem.for\_product(product)  
> > > _think_ I have any typos (the downloaded sample files from the Agiles  
> > > built in the add\_product method of the Cart class, by calling
> 
> > If you find this idiom confusing (I know I do), you may use the return  
> > Cheers,  
> > Louis.

Hi David,

Do you print the class name, instance variables and methods for every  
element of the @items array? I suspect that majority of the array  
contains LineItem objects, but one of the elements is a BigDecimal.

You could try changing the code as I suggested above, deleting all of  
the data from your database, and trying again.

Cheers,  
Louis.
