frankjmattia-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2011-Jul-14 01:40 UTC
what is the difference between using group_by in a scope as opposed the controller?
In my controller I have this and it works. @grouped_by_assignable = collection.order("updated_at desc").group_by { |assignment| assignment.assignable } trying to refactor it into a scope on the model as such scope :grouped_by_assignable, order("updated_at desc").group_by { | assignment| assignment.assignable } yields ArgumentError: Unknown key: #<Project:0x00000102dd5d80> Any advice? Thanks, - FJM -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jim Ruther Nill
2011-Jul-14 04:49 UTC
Re: what is the difference between using group_by in a scope as opposed the controller?
On Thu, Jul 14, 2011 at 9:40 AM, frankjmattia-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org < frankjmattia-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> In my controller I have this and it works. > > @grouped_by_assignable = collection.order("updated_at desc").group_by > { |assignment| assignment.assignable } > > trying to refactor it into a scope on the model as such > > scope :grouped_by_assignable, order("updated_at desc").group_by { | > assignment| assignment.assignable } > > yields > > ArgumentError: Unknown key: #<Project:0x00000102dd5d80> > >group_by is an Array method. You can try doing this instead scope :recently_updated, order("updated_at desc") def self.grouped_by_assignable recently_updated.group_by { |assignment| assignment.assignable } end But beware, this method would return an array not an AR relation> Any advice? > > Thanks, > - FJM > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
frankjmattia-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2011-Jul-14 12:47 UTC
Re: what is the difference between using group_by in a scope as opposed the controller?
> frankjmat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > In my controller I have this and it works. > > > @grouped_by_assignable = collection.order("updated_at desc").group_by > > { |assignment| assignment.assignable } > > > trying to refactor it into a scope on the model as such > > > scope :grouped_by_assignable, order("updated_at desc").group_by { | > > assignment| assignment.assignable } > > > yields > > > ArgumentError: Unknown key: #<Project:0x00000102dd5d80> > > group_by is an Array method. You can try doing this instead > > scope :recently_updated, order("updated_at desc") > > def self.grouped_by_assignable > recently_updated.group_by { |assignment| assignment.assignable } > end > > But beware, this method would return an array not an AR relationAh, makes perfect sense... what could I do if I still wanted to create an AR relation? Is this a use case for #group? I tried that as well but I couldn''t get it to group the way I needed it to... I think that because assignable is polymorphic I need to group by assignable_id and assignable_type... How can I accomplish that? Thanks again, - FJM -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jim Ruther Nill
2011-Jul-15 08:03 UTC
Re: Re: what is the difference between using group_by in a scope as opposed the controller?
On Thu, Jul 14, 2011 at 8:47 PM, frankjmattia-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org < frankjmattia-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > frankjmat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > In my controller I have this and it works. > > > > > @grouped_by_assignable = collection.order("updated_at desc").group_by > > > { |assignment| assignment.assignable } > > > > > trying to refactor it into a scope on the model as such > > > > > scope :grouped_by_assignable, order("updated_at desc").group_by { | > > > assignment| assignment.assignable } > > > > > yields > > > > > ArgumentError: Unknown key: #<Project:0x00000102dd5d80> > > > > group_by is an Array method. You can try doing this instead > > >had to correct this, group_by is an Enumerable method.> > scope :recently_updated, order("updated_at desc") > > > > def self.grouped_by_assignable > > recently_updated.group_by { |assignment| assignment.assignable } > > end > > > > But beware, this method would return an array not an AR relation > > Ah, makes perfect sense... what could I do if I still wanted to create > an AR relation? Is this a use case for #group? I tried that as well > but I couldn''t get it to group the way I needed it to... I think that > because assignable is polymorphic I need to group by assignable_id and > assignable_type... How can I accomplish that? > >I''m pretty sure that you can''t get an array of hashes out of an AR query so I guess this can''t be done on the database side.> Thanks again, > - FJM > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Jul-15 09:36 UTC
Re: what is the difference between using group_by in a scope as opposed the controller?
On Jul 14, 1:47 pm, "frankjmat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <frankjmat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > frankjmat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > In my controller I have this and it works. > > > > @grouped_by_assignable = collection.order("updated_at desc").group_by > > > { |assignment| assignment.assignable } > > > > trying to refactor it into a scope on the model as such > > > > scope :grouped_by_assignable, order("updated_at desc").group_by { | > > > assignment| assignment.assignable } > > > > yields > > > > ArgumentError: Unknown key: #<Project:0x00000102dd5d80> > > > group_by is an Array method. You can try doing this instead > > > scope :recently_updated, order("updated_at desc") > > > def self.grouped_by_assignable > > recently_updated.group_by { |assignment| assignment.assignable } > > end > > > But beware, this method would return an array not an AR relation > > Ah, makes perfect sense... what could I do if I still wanted to create > an AR relation? Is this a use case for #group? I tried that as well > but I couldn''t get it to group the way I needed it to... I think that > because assignable is polymorphic I need to group by assignable_id and > assignable_type... How can I accomplish that? >I would have thought that ''group by assignable_type, assignable_id'' would do the trick Fred> Thanks again, > - FJM-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.