# Undefined method \`\<\<' error

**URL:** <https://www.ruby-forum.com/t/undefined-method-error/48873>\
**Category:** Rails\
**Created:** [November 26, 2005, 1:19am UTC](https://www.ruby-forum.com/t/undefined-method-error/48873 "2005-11-26T01:19:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [November 26, 2005, 1:19am UTC](https://www.ruby-forum.com/t/undefined-method-error/48873/1 "2005-11-26T01:19:53Z")

</div>

Hi. I’m having a strange error. I’m following a book on Ruby on Rails  
and it’s guiding me on how to make a shopping cart.

I personally have double checked all the codes but I get stuck with the  
error:

NoMethodError in Store#add\_to\_cart

undefined method `\<\<’ for #LineItem:0xb6cd2e60

I have add\_to\_cart:

def add\_to\_cart  
product = Product.find(params[:id])  
@cart = find\_cart  
@cart.add\_product(product)  
redirect\_to(:action =\> ‘display\_cart’)  
end

And:

```
    def add_product(product)
            item = LineItem.for_product(product)
            @items << item
            @total_price += product.price
    end

```

and it seems to not like @items \<\< item

Any help is appreciated. I think I’ve checked the code and I couldn’t  
find anything wrong… but i’m sure I’ve done something that I can’t  
catch ☹

Thanks!

---

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [November 26, 2005, 2:50am UTC](https://www.ruby-forum.com/t/undefined-method-error/48873/2 "2005-11-26T02:50:03Z")

</div>

I just redid all the code in the chapter and I get the same thing  
again. It’s as if it doesn’t understand that \<\< means to append or  
something.

Has anyone else had this problem before?

---

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [November 26, 2005, 3:32am UTC](https://www.ruby-forum.com/t/undefined-method-error/48873/3 "2005-11-26T03:32:48Z")

</div>

You may have a typo.  
i am looking at hte hbook and it says:

@items \<\< LineItem.for\_product(product)  
@total\_pice += product.price

hope that helps,  
Mark

On 11/25/05, Khurrum Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

---

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [November 26, 2005, 3:53am UTC](https://www.ruby-forum.com/t/undefined-method-error/48873/4 "2005-11-26T03:53:02Z")

</div>

Thanks for the reply 🙂

I tried it again with the code from the book that you posted but  
unfortunately I get the same error ☹ (sorry I posted the wrong code  
above earlier. I moved it around just to see if it helps and forgot to  
change it back)

undefined method `\<\<’ for #LineItem:0xb6ab7914

So now cart.rb is:

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) # <-- error?
            @total_price += product.price
    end

```

end

This error is very confusing. ☹

heavysixer wrote:

> You may have a typo.  
> i am looking at hte hbook and it says:
> 
> @items \<\< LineItem.for\_product(product)  
> @total\_pice += product.price
> 
> hope that helps,  
> Mark

---

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [November 26, 2005, 4:25am UTC](https://www.ruby-forum.com/t/undefined-method-error/48873/5 "2005-11-26T04:25:29Z")

</div>

Could it have anything to do with the fact that originally the mySQL  
database was myISAM and not InnoDB?

Is there any way to confirm that the contraints are being put on?

---

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [November 26, 2005, 7:50am UTC](https://www.ruby-forum.com/t/undefined-method-error/48873/6 "2005-11-26T07:50:37Z")

</div>

I think that your problem might be with your line item class not the  
cart class.  
I think the error is actually saying it is looking for the “\<\<” method  
inside lineItem. I would check the syntax of your lineItem class  
again. I am using myISAM without troubles.  
Hope that helps,  
Mark

On 11/25/05, Khurrum Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

---

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [November 26, 2005, 5:12pm UTC](https://www.ruby-forum.com/t/undefined-method-error/48873/7 "2005-11-26T17:12:53Z")

</div>

Thanks for the reply 🙂

I just noticed that it was working in IE but not firefox so I deleted  
the cookies that I had from my domain and it started working ☹

hehe a day was wasted hehe but I guess I learned something.

Thanks for the help 🙂

heavysixer wrote:

> I think that your problem might be with your line item class not the  
> cart class.  
> I think the error is actually saying it is looking for the “\<\<” method  
> inside lineItem. I would check the syntax of your lineItem class  
> again. I am using myISAM without troubles.  
> Hope that helps,  
> Mark
> 
> On 11/25/05, Khurrum Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> –
