Hi all... I''m trying to create a named_scope for people who were born this year. I have an application helper method defining "current_year". So far, I have this, but it''s not filtering correctly. I think I need a BETWEEN, maybe? I know this is something easy I''m just not seeing... any help is appreciated! class Person < ActiveRecord::Base :named_scope :born_this_year, :conditions => {:dob => :current_year} end -- 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 Sun, Nov 14, 2010 at 7:35 PM, KT <malokt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m trying to create a named_scope for people who were born this > year. I have an application helper method defining "current_year". > > So far, I have this, but it''s not filtering correctly. I think I need > a BETWEEN, maybe? I know this is something easy I''m just not > seeing... any help is appreciated! > > class Person < ActiveRecord::BaseHave you used a debugger or logging statements to see what the values of :dob and :current_year are at this point? :-)> :named_scope :born_this_year, :conditions => {:dob => :current_year} > end-- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan -- 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.
On Mon, Nov 15, 2010 at 11:35 AM, KT <malokt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all... > > I''m trying to create a named_scope for people who were born this > year. I have an application helper method defining "current_year". > > So far, I have this, but it''s not filtering correctly. I think I need > a BETWEEN, maybe? I know this is something easy I''m just not > seeing... any help is appreciated! > > class Person < ActiveRecord::Base > :named_scope :born_this_year, :conditions => {:dob => :current_year} > endAssuming your :dob is a date Try :conditions => [''dob BETWEEN ? AND ?'', Date.today.beginning_of_year, Date.today.end_of_year] -- Erol M. Fornoles http://github.com/Erol http://twitter.com/erolfornoles http://ph.linkedin.com/in/erolfornoles -- 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 Mon, Nov 15, 2010 at 6:24 PM, Erol Fornoles <erol.fornoles-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> Assuming your :dob is a date > > Try :conditions => [''dob BETWEEN ? AND ?'', Date.today.beginning_of_year, > Date.today.end_of_year] >Wait let me correct that, but first, is :current_year a field or column in your table or a parameter that was meant to be passed to the named_scope? -- Erol M. Fornoles http://github.com/Erol http://twitter.com/erolfornoles http://ph.linkedin.com/in/erolfornoles -- 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.
Erol Fornoles wrote in post #961514:> On Mon, Nov 15, 2010 at 11:35 AM, KT <malokt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> :named_scope :born_this_year, :conditions => {:dob => :current_year} >> end > > > Assuming your :dob is a date > > Try :conditions => [''dob BETWEEN ? AND ?'', Date.today.beginning_of_year, > Date.today.end_of_year]Even better: :conditions => [''year(dob) = ?'', Date.today.year]> > -- > Erol M. FornolesBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.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.
Thanks for your responses everyone. Sorry for the delay in re- posting, I''ve been away from computer. @Erol :current_year is an application helper method to pull the current year! I''ll try your suggestions, as well and report back. Thank you @Hassan I will try a debugger, too - thanks for the reminder. On Nov 15, 2:31 am, Erol Fornoles <erol.forno...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mon, Nov 15, 2010 at 6:24 PM, Erol Fornoles <erol.forno...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: > > > Assuming your :dob is a date > > > Try :conditions => [''dob BETWEEN ? AND ?'', Date.today.beginning_of_year, > > Date.today.end_of_year] > > Wait let me correct that, but first, is :current_year a field or column in > your table or a parameter that was meant to be passed to the named_scope? > > -- > Erol M. Fornoleshttp://github.com/Erolhttp://twitter.com/erolfornoleshttp://ph.linkedin.com/in/erolfornoles-- 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.
Thanks all -. I ended up having to use a lambda: named_scope :born_this_year, lambda { |year| { :conditions => [''dob between ? and ?'', Date.today.beginning_of_year, Date.today.end_of_year]}} I love this group. :) On Nov 15, 6:33 pm, KT <mal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks for your responses everyone. Sorry for the delay in re- > posting, I''ve been away from computer. > > @Erol :current_year is an application helper method to pull the > current year! > I''ll try your suggestions, as well and report back. Thank you > > @Hassan I will try a debugger, too - thanks for the reminder. > > On Nov 15, 2:31 am, Erol Fornoles <erol.forno...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Mon, Nov 15, 2010 at 6:24 PM, Erol Fornoles <erol.forno...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: > > > > Assuming your :dob is a date > > > > Try :conditions => [''dob BETWEEN ? AND ?'', Date.today.beginning_of_year, > > > Date.today.end_of_year] > > > Wait let me correct that, but first, is :current_year a field or column in > > your table or a parameter that was meant to be passed to the named_scope? > > > -- > > Erol M. Fornoleshttp://github.com/Erolhttp://twitter.com/erolfornoleshttp://ph.linked...-- 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.