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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2006-Aug-25 04:07 UTC
[Rails] Re: How to show if there are children in a list?
Larry Kelly wrote:> 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?You can use Rails'' automatic association size caching by adding a children_count field to the parents table, then testing whether parent.children_count == 0. Otherwise you''d have to add :joins, :group, and :select options to your find call to add a attribute to each parent instance that reflects the number of or presence of children. -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 On Aug 24, 2006, at 11:26 AM, Larry Kelly wrote:> 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 > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---