How to show if there are children in a list?

I have the following parent - child models.

class Parent
has_many :children
end

class Child
belongs_to :parent, :foreign_key => “parent_id”
end

Now, in my list of parents, the user can click a link to see the
children
for a parent. What I’d want to do is to show whether a parent has any
children on the parent list page, without the user having to click on
the
link to find out.

My current query to populate the table is as follows:

@parents = Parent.find(:all, :order => “name”)

Can this query be modified to get the number of children as well? Or,
return true if the parent has at least one child?


Best Regards,
-Larry
“Work, work, work…there is no satisfactory alternative.”
— E.Taft Benson

In the controller:
@parents = Parent.find(:all, :order => “name”, :include => :children)

In the view (while iterating through @parents):
<%= ‘Has children’ if parent.children.any? %>
or
<%= “Has #{pluralize(parent.children.count, ‘child’, ‘children’)}” %>


Building an e-commerce site with Rails?
http://www.agilewebdevelopment.com/rails-ecommerce