Sorry if this is a duplicate post, but Google Groups is having issues again. :( I have the following named scope in a Rails 3.0.5 model: scope :past_due, where(''NOT complete AND requested_start_date < ?'', Date.today). order(''requested_start_date ASC'') and I''d like to have a named scope that reuses the :past_due scope to return a count of matching records. It''s easy to do in the controller, but I can''t seem to find the right incantation to create a :past_due_count scope in the model. What is the right way to do this? -- 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.
Phil On Fri, Mar 11, 2011 at 4:50 PM, Todd A. Jacobs < codegnome.consulting-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Sorry if this is a duplicate post, but Google Groups is having issues > again. :( > > I have the following named scope in a Rails 3.0.5 model: > > scope :past_due, > where(''NOT complete AND requested_start_date < ?'', > Date.today). > order(''requested_start_date ASC'') > > and I''d like to have a named scope that reuses the :past_due scope to > return a count of matching records. It''s easy to do in the controller, > but I can''t seem to find the right incantation to create > a :past_due_count scope in the model. > > What is the right way to do this? >You usually don''t use a scope to return a count; you use it to return an array, some subset of #all. If you just need the count of the same scope, won''t Model.past_due.count work?> > -- > 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. > >-- 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.
On Mar 11, 10:50 pm, "Todd A. Jacobs" <codegnome.consult...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Sorry if this is a duplicate post, but Google Groups is having issues > again. :( > > I have the following named scope in a Rails 3.0.5 model: > > scope :past_due, > where(''NOT complete AND requested_start_date < ?'', > Date.today). > order(''requested_start_date ASC'') > > and I''d like to have a named scope that reuses the :past_due scope to > return a count of matching records. It''s easy to do in the controller, > but I can''t seem to find the right incantation to create > a :past_due_count scope in the model. > > What is the right way to do this?Just call past_due.count. A scope is basically a set of query options (conditions, joins, orders etc) that can then be used to restrict the scope of actions such as updating, finding records etc, there isn''t really such a thing as a count scope (I suppose you could add select(''count(*)'') to a scope but that would just be a bit perverse. Fred -- 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.