# Syntax for calling the has\_many class methods

**URL:** <https://www.ruby-forum.com/t/syntax-for-calling-the-has-many-class-methods/71803>\
**Category:** Rails\
**Created:** [October 11, 2006, 4:37pm UTC](https://www.ruby-forum.com/t/syntax-for-calling-the-has-many-class-methods/71803 "2006-10-11T16:37:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [October 11, 2006, 4:37pm UTC](https://www.ruby-forum.com/t/syntax-for-calling-the-has-many-class-methods/71803/1 "2006-10-11T16:37:40Z")

</div>

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?

Thanks,  
Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [October 11, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/syntax-for-calling-the-has-many-class-methods/71803/2 "2006-10-11T16:49:57Z")

</div>

On 10/11/06, Jeff [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> 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.

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [October 11, 2006, 4:55pm UTC](https://www.ruby-forum.com/t/syntax-for-calling-the-has-many-class-methods/71803/3 "2006-10-11T16:55:59Z")

</div>

#line\_items is an instance methods. You can’t call them on a Class  
because  
there is no link between the models. Try to think of it through the  
resulting SQL queries.

self.line\_items – find all line\_items with a order\_id of self.id

You have to think of what ‘Order.line\_items \<\< li’ means. You can’t do  
Order.line\_items because that would be like saying “find all line\_items  
with  
order\_id of Class.id” which is obviously wrong. Line\_items are linked to  
Orders and so you need to have an order to find line\_items.

Just keep in mind that all method calls on ActiveRecord objects are in  
fact  
queries to the database (or cached queries).

Hope that helped.

Jason

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [October 11, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/syntax-for-calling-the-has-many-class-methods/71803/4 "2006-10-11T17:46:27Z")

</div>

I get it, thanks. I misunderstood the API documentation. It explains  
that has\_many, et al are class methods. Then it uses a diagram syntax  
like this to describe the methods that has\_many creates:

Firm#clients\<\<

Class#method is a syntax used to illustrate class methods in the  
PickAxe book. So I assumed clients\<\< was a class method, when in fact  
it is an instance method.

Thanks again,  
Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [October 11, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/syntax-for-calling-the-has-many-class-methods/71803/5 "2006-10-11T18:24:40Z")

</div>

Hi –

On Wed, 11 Oct 2006, Jeff wrote:

> I get it, thanks. I misunderstood the API documentation. It explains  
> that has\_many, et al are class methods. Then it uses a diagram syntax  
> like this to describe the methods that has\_many creates:
> 
> Firm#clients\<\<
> 
> Class#method is a syntax used to illustrate class methods in the  
> PickAxe book. So I assumed clients\<\< was a class method, when in fact  
> it is an instance method.

The #method notation is actually for an instance method, like  
String#split. Class methods are usually indicated like this:  
ActiveRecord::Base.has\_many.

But if the class whose instance method it is is Class, you’d get  
Class#method… So, for example, it’s correct to refer to  
Class#new, since new is an instance method of class.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [October 11, 2006, 7:15pm UTC](https://www.ruby-forum.com/t/syntax-for-calling-the-has-many-class-methods/71803/6 "2006-10-11T19:15:29Z")

</div>

Thanks David. You’re right. The PickAxe does use Class#method to  
describe an instance method. I’m mentally challenged today!

– Jeff
