How to get a count of line_items

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

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

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/ 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

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.