Learning to degug? where do I start

I’m using rails to learn ruby so sorry if this is a really dumb
question. I"m getting this output but I don’t know where to look for
the problem. Here is the error output from the log.

ActionView::TemplateError (undefined method `to_f’ for
#Product:0xb7906f88) on line #16 of
app/views/store/display_cart.rhtml:
13:


14: <%= item.quantity %>
15: <%= h(product.title) %>
16: <%= item.unit_price %>
17: <%= item.unit_price * item.quantity %>
18:
19: <% end -%>
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/base.rb:1501:in 

method_missing' (eval):1:inunit_price’

I know that to_f tries to make a variable into a float but I don’t know
what is trying to do this.

How is item.unit_price implemented?

It could be that the error message is a bit misleading - the error may
not be the .rhtml code itself but inside the unit_price method
somewhere.

Just a guess.

Here is my model:

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
item
end

end

On Dec 13, 2005, at 11:48 PM, charlie bowman wrote:

16:

<%= item.unit_price %>
I know that to_f tries to make a variable into a float but I don’t
know
what is trying to do this.

I’d guess it’s the multiplication that’s trying to coerce the object
types,
but that’s on line 17.

In any case, I’d try to change your model:

from…
item.quantity = 1

to…
item.quantity = 1.0


– Tom M.

I made that change (quantity = 1.0) and it didn’t help. I also greped
the whole folder for mentions of “unit” or “quantitiy”. My only
references to these are the Model I pasted in earlier and in the
template. Where should I be looking for the problem. I’m too new at
ruby to know how to find the error.

ActionView::TemplateError (undefined method `to_f’ for
#Product:0xb7906f88) on line #16 of
app/views/store/display_cart.rhtml:
13:


14: <%= item.quantity %>
15: <%= h(product.title) %>
16: <%= item.unit_price %>
17: <%= item.unit_price * item.quantity %>
18:
19: <% end -%>
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/base.rb:1501:in

method_missing' (eval):1:inunit_price’

I know that to_f tries to make a variable into a float but I don’t know
what is trying to do this.

Tom M. wrote:

On Dec 13, 2005, at 11:48 PM, charlie bowman wrote:

16:

<%= item.unit_price %>
I know that to_f tries to make a variable into a float but I don’t
know
what is trying to do this.

I’d guess it’s the multiplication that’s trying to coerce the object
types,
but that’s on line 17.

In any case, I’d try to change your model:

from…
item.quantity = 1

to…
item.quantity = 1.0


– Tom M.

mysql> describe line_items;
±-----------±--------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±-----------±--------------±-----±----±--------±---------------+
| id | int(11) | | PRI | NULL | auto_increment |
| product_id | int(11) | | MUL | 0 | |
| quantity | int(11) | | | 0 | |
| unit_price | decimal(10,2) | | | 0.00 | |
±-----------±--------------±-----±----±--------±---------------+

mysql> describe products;
±------------±--------------------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±------------±--------------------------±-----±----±--------±---------------+
| id | int(11) | | PRI | NULL | auto_increment |
| title | varchar(100) | | | | |
| description | text | | | | |
| image_url | varchar(200) | | | | |
| price | decimal(10,2) | | | 0.00 | |
| size | varchar(50) | | | | |
| sold | enum(‘current’,‘gallery’) | | | current | |
±------------±--------------------------±-----±----±--------±---------------+
7 rows in set (0.00 sec)

Jeremy M. wrote:

What does the database schema look like for your line_items and products
tables?

I’ll bet if you looked at the class type for ‘item.quantity’ and
‘item.unit_price’ they would not be what you expect (fixnum and float
respectively).

Add these lines to your view:

<%= debug(item.quantity.class) %>

<%= debug(item.unit_price.class) %>

My test showed ‘item.quantity’ as a fixnum but ‘item.unit_price’ as a
string. To fix it I converted ‘item.unit_price’ to float:

<%= item.unit_price.to_f * item.quantity %>

What does the database schema look like for your line_items and products
tables?

I’ve narrowed it down a little. The page will display as long I don’t
mention unit_price. For somereason that throws it off.

thanks for the help but still no headway, here is my out put now. What
seems weird is the dump I get. The last section is a snippet of the
session dump that looks odd to me.

NoMethodError in Store#display_cart

Showing app/views/store/display_cart.rhtml where line #14 raised:

undefined method `to_f’ for #Product:0xb79e9e40

Extracted source (around line #14):

11: product = item.product
12: -%>
13: <%= debug(item.quantity.class) %>

14: <%= debug(item.unit_price.class) %>
15:
16: <%= item.quantity %>
17: <%= h(product.title) %>

RAILS_ROOT: script/…/config/…
usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1501:in
method_missing' (eval):1:in unit_price’
#{RAILS_ROOT}/app/views/store/display_cart.rhtml:14
#{RAILS_ROOT}/app/views/store/display_cart.rhtml:10

##session dump
!ruby/object:LineItem
attributes:
quantity: 1
product_id: 4
unit_price: &id002 !ruby/object:Product
attributes:
image_url: http://localhost:3000/images/twin_fairies_small.gif
size: 12 inches by 18 inches

Jeremy M. wrote:

I’ll bet if you looked at the class type for ‘item.quantity’ and
‘item.unit_price’ they would not be what you expect (fixnum and float
respectively).

Add these lines to your view:

<%= debug(item.quantity.class) %>

<%= debug(item.unit_price.class) %>

My test showed ‘item.quantity’ as a fixnum but ‘item.unit_price’ as a
string. To fix it I converted ‘item.unit_price’ to float:

<%= item.unit_price.to_f * item.quantity %>

Thanks for posting the unusual solution to this Charlie. I just
encountered the same problem.

Sound advice worthy of fine-print note on the bottom of a page in ‘Agile
Web D. With Rails’.

charlie bowman wrote:

Turns out it was a bug in the built in server. I restarted the server
and all my problems stopped. Thanks to all for thier advice! I think
I’m going to like this rails stuff!

Turns out it was a bug in the built in server. I restarted the server
and all my problems stopped. Thanks to all for thier advice! I think
I’m going to like this rails stuff!

charlie bowman wrote:

I’ve narrowed it down a little. The page will display as long I don’t
mention unit_price. For somereason that throws it off.