# Assignment for has\_many objects

**URL:** <https://www.ruby-forum.com/t/assignment-for-has-many-objects/111718>\
**Category:** Rails\
**Created:** [August 10, 2007, 2:04am UTC](https://www.ruby-forum.com/t/assignment-for-has-many-objects/111718 "2007-08-10T02:04:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f9ae1b/32.png) [@jub](https://www.ruby-forum.com/u/jub)\
**Post date:** [August 10, 2007, 2:04am UTC](https://www.ruby-forum.com/t/assignment-for-has-many-objects/111718/1 "2007-08-10T02:04:49Z")

</div>

Hello,

I’m playing with Agile Web dev for rails, inside which I encountered  
this model :

class Order \< ActiveRecord::Base  
has\_many :line\_items  
PAYMENT\_TYPES = [  
# Displayed stored in db  
[“Check” , “check”],  
[“Credit card” , “cc”],  
[“Purchase order” , “po”]  
]  
# …  
validates\_presence\_of :name, :address, :email, :pay\_type  
validates\_inclusion\_of :pay\_type, :in =\> PAYMENT\_TYPES.map {|disp,  
value| value}

# …

def add\_line\_items\_from\_cart(cart)  
cart.items.each do |item|  
li = LineItem.from\_cart\_item(item)  
line\_items \<\< li  
end  
end  
end

So far, I know that has\_many adds several methods, such as line\_items\<\<  
and line\_items=.  
I also know that, inside a method, an assignment to some “var” without  
“self.” before it might confuse ruby which can take “var” for a local  
variable. So using self.var is “safer”.

But here, there’s no “self.line\_items \<\< li”. Why does calling  
“line\_items \<\<” is enough for this piece of code to understand that  
line\_items isn’t a mere variable ?

I know that there are some particular cases where “self.” isn’t  
necessary. Maybe are we in one of them ?

Thanks

---

<div class="post-metadata">

**Author:** ![jub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f9ae1b/32.png) [@jub](https://www.ruby-forum.com/u/jub)\
**Post date:** [August 10, 2007, 6:02pm UTC](https://www.ruby-forum.com/t/assignment-for-has-many-objects/111718/2 "2007-08-10T18:02:05Z")

</div>

Arnaud J. wrote:

> line\_items isn’t a mere variable ?  
> “self” is not needed here because the line is not an assignment  
> (no “=”). Unlike an assignment, for the line to have any meaning  
> line\_items must have been defined previously, so the interpreter  
> sees no ambiguity.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![jub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f9ae1b/32.png) [@jub](https://www.ruby-forum.com/u/jub)\
**Post date:** [August 10, 2007, 6:14pm UTC](https://www.ruby-forum.com/t/assignment-for-has-many-objects/111718/3 "2007-08-10T18:14:07Z")

</div>

Mark Reginald J. wrote:

> “self” is not needed here because the line is not an assignment  
> (no “=”). Unlike an assignment, for the line to have any meaning  
> line\_items must have been defined previously, so the interpreter  
> sees no ambiguity.

Got it. I’m not quite used to Ruby yet, that’s why. To me, calling  
“some\_array \<\< 1” was like having an implicit “some\_array = []”. But it  
doesn’t work like that.

Thank you very much.
