On Wed, Oct 22, 2008 at 11:51 AM, Sven <Sven.Aas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> ... > > Is there a better way to accomplish this using named_scopes?My first thought is to define the named_scope as you have and also define a wrapper method. In the wrapper method, you can make the time parameter optional with a default of Time.now. Then you''d just use the wrapper method instead of the named_scope directly. I don''t know how well that would play with chaining of named_scopes. Regards, Craig --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
So something like this, perhaps:
class Post < ActiveRecord::Base
named_scope :current_as_of, lambda { |time|
{ :conditions => [''do_not_show_before < ?'', time]
}
}
def self.current(time = Time.now)
self.current_as_of(time)
end
end
I''m also concerned about chaining, and in particular about the
behavior of things like will_paginate. Hmmm.
Thanks,
Sven
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Actually, you could do something like this:
named_scope :current, lambda { |*args| { :conditions =>
[''do_not_show_before < ?'', (args.first || Time.now) ] } }
I knew Ryan Bates'' screencast on named_scope covered defaults. See:
http://railscasts.com/episodes/108 .
Regards,
Craig
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Yes he did. I hadn''t seen that one and didn''t think of checking it. Ryan suggests exactly what you''ve proposed; I''ve tried it and it works. Thank you! This is much cleaner, and I get to keep using the named_scope instead of a method. Now if I can just find a way around my other method-to-named_scope workaround problem (http://groups.google.com/group/rubyonrails-talk/ browse_thread/thread/a12f471c54a26b20) my code will be consistent once again :) -Sven --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Wed, Oct 22, 2008 at 12:57 PM, Sven <Sven.Aas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Yes he did. I hadn''t seen that one and didn''t think of checking it. > Ryan suggests exactly what you''ve proposed; I''ve tried it and it > works. Thank you! This is much cleaner, and I get to keep using the > named_scope instead of a method.Just to be sure Ryan gets the credit, I checked his site and then modified your code according to his example. :-) Craig --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---