Re: Syntax for calling the has_many class methods

Michael Campbell
Wed, 11 Oct 2006 14:41:29 -0000

On 10/11/06, Jeff <objectis-/E1597aS9LQAvxtiuMwx3w@public.gmane.org>
wrote:
>
> I have a simple Active Record model:
>
> class Order < ActiveRecord::Base
>   has_many  :line_items
>   def add_line_items_from_cart(cart)
>     cart.items.each do |item|
>       li = LineItem.from_cart_item(item)
>       line_items << li
>     end
>   end
> end
>
> This syntax works fine:
>  line_items << li
>
> This also works:
>  self.line_items << li
>
> But this syntax fails with an error (undefined method `line_items''
for
> Order:Class):
>  Order.line_items << li
>
> Why the error?


I think because you''re trying to call something on the Order /class/,
rather than on an instance OF an Order.