# How to get a count of line\_items

**URL:** <https://www.ruby-forum.com/t/how-to-get-a-count-of-line-items/72005>\
**Category:** Rails\
**Created:** [October 13, 2006, 2:49pm UTC](https://www.ruby-forum.com/t/how-to-get-a-count-of-line-items/72005 "2006-10-13T14:49:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 13, 2006, 2:49pm UTC](https://www.ruby-forum.com/t/how-to-get-a-count-of-line-items/72005/1 "2006-10-13T14:49:06Z")

</div>

I am fairly new to Rails, and I am having difficulty in forgetting the  
old way of developing and using the Rails way.

This is my question, using orders and line\_items as an example.  
An order has\_many line\_items  
A line\_item belongs to order

In my OrdersController I have

def summary  
@orders = Orders.find(:all)  
render :xml =\> @orders.to\_xml()  
end

What I want to achieve is to include a count of line\_items per order,  
and to include it as say total\_line\_items in the output.

I don’t know how to a.) get the count per order, b.) how to include in  
the output.

Thanks

Andrew

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 13, 2006, 7:10pm UTC](https://www.ruby-forum.com/t/how-to-get-a-count-of-line-items/72005/2 "2006-10-13T19:10:42Z")

</div>

I haven’t worked with render :xml, so I can’t help with b), though I  
expect you’ll have to step back and create your xml document manually  
using REXML or Builder instead of using the to\_xml method. to\_xml is  
easy, but limited when you want to add information that is not  
specifically part of the schema, but Builder is a cinch to use, too, so  
you won’t have much ramp-up.

As to a)… assuming you have an order object in @order, then you can  
get the count of line\_items with:

@order.line\_items.size

Using Builder, you might include this in your xml document with  
something like:

xml.total\_line\_items @order.line\_items.size.to\_s

Which should result in something like:

\<total\_line\_items\>72\</total\_line\_items\>

c.

Andrew C. wrote:

> I am fairly new to Rails, and I am having difficulty in forgetting the  
> old way of developing and using the Rails way.
> 
> This is my question, using orders and line\_items as an example.  
> An order has\_many line\_items  
> A line\_item belongs to order
> 
> In my OrdersController I have
> 
> def summary  
> @orders = Orders.find(:all)  
> render :xml =\> @orders.to\_xml()  
> end
> 
> What I want to achieve is to include a count of line\_items per order,  
> and to include it as say total\_line\_items in the output.
> 
> I don’t know how to a.) get the count per order, b.) how to include in  
> the output.
> 
> Thanks
> 
> Andrew

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 13, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/how-to-get-a-count-of-line-items/72005/3 "2006-10-13T21:59:02Z")

</div>

Hi Andrew,

Andrew C.

> What I want to achieve is to include a count of  
> line\_items per order, and to include it as say  
> total\_line\_items in the output.
> 
> I don’t know how to a.) get the count per order,

I think you’ll find what you need at [http://api.rubyonrails.org/](http://api.rubyonrails.org/) in the  
methods pane (lower left) under ‘count’.

> b.) how to include in the output.

This will depend on what, exactly, you want the output to look like.  
to\_xml  
is very limited. Except in very unusual cases, I personally don’t think  
it’s  
a great idea to couple an app’s output so tightly to the application’s  
internal data representation. YMMV. to\_xml may or may not give you  
the  
flexibility you need. Builder is so incredibly simple to use that it’s  
definitely worth looking at right from the ‘git go’ IMHO. The output  
you  
decide on could / probably will have an impact on your decision re: how  
exactly to approach getting the counts you’re looking for.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 13, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/how-to-get-a-count-of-line-items/72005/4 "2006-10-13T19:55:39Z")

</div>

Not sure but you’ll probably have to cycle through the orders and count  
the line items for each.

@orders.each do {|order| order[‘total\_line\_items’] =  
order.line\_items.size}

That will create a new attribute on your order objects (but not in the  
database) for the number of line items. Hopefully render :xml will know  
what to do with it.

Cayce B. wrote:

> I haven’t worked with render :xml, so I can’t help with b), though I  
> expect you’ll have to step back and create your xml document manually  
> using REXML or Builder instead of using the to\_xml method. to\_xml is  
> easy, but limited when you want to add information that is not  
> specifically part of the schema, but Builder is a cinch to use, too, so  
> you won’t have much ramp-up.
> 
> As to a)… assuming you have an order object in @order, then you can  
> get the count of line\_items with:
> 
> @order.line\_items.size
> 
> Using Builder, you might include this in your xml document with  
> something like:
> 
> xml.total\_line\_items @order.line\_items.size.to\_s
> 
> Which should result in something like:
> 
> \<total\_line\_items\>72\</total\_line\_items\>
> 
> c.
> 
> Andrew C. wrote:
> 
> > I am fairly new to Rails, and I am having difficulty in forgetting the  
> > old way of developing and using the Rails way.
> > 
> > This is my question, using orders and line\_items as an example.  
> > An order has\_many line\_items  
> > A line\_item belongs to order
> > 
> > In my OrdersController I have
> > 
> > def summary  
> > @orders = Orders.find(:all)  
> > render :xml =\> @orders.to\_xml()  
> > end
> > 
> > What I want to achieve is to include a count of line\_items per order,  
> > and to include it as say total\_line\_items in the output.
> > 
> > I don’t know how to a.) get the count per order, b.) how to include in  
> > the output.
