Hi, I want the find(:all) method to only return published Comments. This works: # Override find method to only return published comments def self.find(*args) options = args.extract_options! validate_find_options(options) set_readonly_option!(options) case args.first when :first then find_initial(options) when :last then find_last(options) when :all then options[:conditions] = { :published => true }; find_every(options) else find_from_ids(args, options) end end But is not very DRY since I pasted the code from active_record/base.rb. Any improvements on this? CmdJohnson --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Commander Johnson wrote:> I want the find(:all) method to only return published Comments.Use a named_scope. It''s too easy like that. -- Phlip http://flea.sourceforge.net/resume.html
Commander Johnson
2009-Apr-25 14:18 UTC
Re: How to properly override ActiveRecord::Base.find
Indeed! named_scope :only_published, :conditions => { :published => true } On Sat, Apr 25, 2009 at 2:48 PM, Phlip <phlip2005-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Commander Johnson wrote: > > > I want the find(:all) method to only return published Comments. > > Use a named_scope. It''s too easy like that. > > -- > Phlip > http://flea.sourceforge.net/resume.html > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Apr-25 14:57 UTC
Re: How to properly override ActiveRecord::Base.find
On Apr 25, 3:18 pm, Commander Johnson <commanderjohn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Indeed! > > named_scope :only_published, :conditions => { :published => true } >If you''re on 2.3 you may also be interested in the default_scope stuff that was recently added. Fred> On Sat, Apr 25, 2009 at 2:48 PM, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Commander Johnson wrote: > > > > I want the find(:all) method to only return published Comments. > > > Use a named_scope. It''s too easy like that. > > > -- > > Phlip > > http://flea.sourceforge.net/resume.html
Commander Johnson
2009-Apr-28 17:19 UTC
Re: How to properly override ActiveRecord::Base.find
I did use that initially, but it broke my associations. After all it was the least work to just stick with a named_scope and double-check frontend controllers for not showing unpublished items. On Sat, Apr 25, 2009 at 4:57 PM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Apr 25, 3:18 pm, Commander Johnson <commanderjohn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > Indeed! > > > > named_scope :only_published, :conditions => { :published => true } > > > If you''re on 2.3 you may also be interested in the default_scope stuff > that was recently added. > > Fred > > On Sat, Apr 25, 2009 at 2:48 PM, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Commander Johnson wrote: > > > > > > I want the find(:all) method to only return published Comments. > > > > > Use a named_scope. It''s too easy like that. > > > > > -- > > > Phlip > > > http://flea.sourceforge.net/resume.html > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hey man, I used something like this with great success! (so far... since I just used like few minutes ago, nothing broke and all test passed) In Rails 2.2.2 class << self def find(*args) args.first == :all ? self.with_scope(:find => {:conditions => [:published => true]}) {super} : super end end Do test it out if you still need to. And anyone else could comment if this is the right way to do it. Cheers! Arzumy MD On Apr 29, 1:19 am, Commander Johnson <commanderjohn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I did use that initially, but it broke my associations. After all it was the > least work to just stick with a named_scope and double-check frontend > controllers for not showing unpublished items. > > On Sat, Apr 25, 2009 at 4:57 PM, Frederick Cheung < > > frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Apr 25, 3:18 pm, Commander Johnson <commanderjohn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > Indeed! > > > > named_scope :only_published, :conditions => { :published => true } > > > If you''re on 2.3 you may also be interested in the default_scope stuff > > that was recently added. > > > Fred > > > On Sat, Apr 25, 2009 at 2:48 PM, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Commander Johnson wrote: > > > > > > I want the find(:all) method to only return published Comments. > > > > > Use a named_scope. It''s too easy like that. > > > > > -- > > > > Phlip > > > > http://flea.sourceforge.net/resume.html