I was reading the book of Dave Thomas, "Agile Web Development with Rails", Fourth Edition. Inside a class named Cart, he called an attribute named "line_items" provided by a relationship between the classes Cart and LineItem.
Inside the method that uses this attribute int the Cart class, the attribute is called without the "@" that is used when we invoke the attributes of a instance. Can someone say to me why this work? Because I was expecting that he had used the "@" symbol. The code is shown below:
class Cart < ActiveRecord::Base
has_many :line_items, :dependent => :destroy
def add_product(product_id)
#In this line below, I was expecting @lineitems.find_by .....
current_item = line_items.find_by_product_id(product_id)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(:product_id => product_id)
end
current_item
end
end