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