Say I have A which has many B which has many C, ideally I want the quickest way to get the count of C for A, a counter cache through an association with B would do - but I don''t think that exists. Any ideas? --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
has_many :cs, :through => :b, :counter_cache => true Does this work? On Jan 9, 2008 8:48 AM, DEfusion <david.spurr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Say I have A which has many B which has many C, ideally I want the > quickest way to get the count of C for A, a counter cache through an > association with B would do - but I don''t think that exists. Any ideas? > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
No the counter_cache is on the belongs_to and if I try adding through on the belongs to then it whinges, e.g. on C: belongs_to :a, through => :b, :counter_cache => true I get the error undefined local variable or method ''through'' -D On Jan 8, 10:51 pm, "Ryan Bigg" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> has_many :cs, :through => :b, :counter_cache => true > > Does this work? > > On Jan 9, 2008 8:48 AM, DEfusion <david.sp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Say I have A which has many B which has many C, ideally I want the > > quickest way to get the count of C for A, a counter cache through an > > association with B would do - but I don''t think that exists. Any ideas? > > -- > Ryan Bigghttp://www.frozenplague.net > Feel free to add me to MSN and/or GTalk as this email.--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Maybe it should be :through and not through? You''re missing a colon. But I dont think belongs_to has a through option. On Jan 9, 2008 9:41 AM, DEfusion <david.spurr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > No the counter_cache is on the belongs_to and if I try adding through > on the belongs to then it whinges, e.g. on C: > > belongs_to :a, through => :b, :counter_cache => true > > I get the error undefined local variable or method ''through'' > > -D > > On Jan 8, 10:51 pm, "Ryan Bigg" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > has_many :cs, :through => :b, :counter_cache => true > > > > Does this work? > > > > On Jan 9, 2008 8:48 AM, DEfusion <david.sp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > Say I have A which has many B which has many C, ideally I want the > > > quickest way to get the count of C for A, a counter cache through an > > > association with B would do - but I don''t think that exists. Any > ideas? > > > > -- > > Ryan Bigghttp://www.frozenplague.net > > Feel free to add me to MSN and/or GTalk as this email. > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
What I''ve ended up doing is the following class C < ActiveRecord::Base belongs_to :B after_create :increment_A_counter_cache after_destroy :decrement_A_counter_cache private def increment_A_counter_cache A.increment_counter( ''c_count'', self.B.A.id ) end def decrement_A_counter_cache A.decrement_counter( ''c_count'', self.B.A.id ) end end It seems to work, I was just wondering if there was a way to do it through the framework. -D On Jan 8, 11:11 pm, DEfusion <david.sp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> No the counter_cache is on the belongs_to and if I try adding through > on the belongs to then it whinges, e.g. on C: > > belongs_to :a, through => :b, :counter_cache => true > > I get the error undefined local variable or method ''through'' > > -D > > On Jan 8, 10:51 pm, "Ryan Bigg" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > has_many :cs, :through => :b, :counter_cache => true > > > Does this work? > > > On Jan 9, 2008 8:48 AM, DEfusion <david.sp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Say I have A which has many B which has many C, ideally I want the > > > quickest way to get the count of C for A, a counter cache through an > > > association with B would do - but I don''t think that exists. Any ideas? > > > -- > > Ryan Bigghttp://www.frozenplague.net > > Feel free to add me to MSN and/or GTalk as this email.--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Not currently. Do you feel confident enough to suggest this as a patch to rails? On Jan 9, 2008 9:59 AM, DEfusion <david.spurr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > What I''ve ended up doing is the following > > class C < ActiveRecord::Base > belongs_to :B > > after_create :increment_A_counter_cache > after_destroy :decrement_A_counter_cache > > private > > def increment_A_counter_cache > A.increment_counter( ''c_count'', self.B.A.id ) > end > > def decrement_A_counter_cache > A.decrement_counter( ''c_count'', self.B.A.id ) > end > end > > It seems to work, I was just wondering if there was a way to do it > through the framework. > > -D > > On Jan 8, 11:11 pm, DEfusion <david.sp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > No the counter_cache is on the belongs_to and if I try adding through > > on the belongs to then it whinges, e.g. on C: > > > > belongs_to :a, through => :b, :counter_cache => true > > > > I get the error undefined local variable or method ''through'' > > > > -D > > > > On Jan 8, 10:51 pm, "Ryan Bigg" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > has_many :cs, :through => :b, :counter_cache => true > > > > > Does this work? > > > > > On Jan 9, 2008 8:48 AM, DEfusion <david.sp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Say I have A which has many B which has many C, ideally I want the > > > > quickest way to get the count of C for A, a counter cache through an > > > > association with B would do - but I don''t think that exists. Any > ideas? > > > > > -- > > > Ryan Bigghttp://www.frozenplague.net > > > Feel free to add me to MSN and/or GTalk as this email. > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---