Forum: Ruby on Rails How to properly override ActiveRecord::Base.find

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7fe5937b838b580eeaa061c85f700583?d=identicon&s=25 Commander Johnson (Guest)
on 2009-04-25 14:33
(Received via mailing list)
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
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-04-25 14:49
(Received via mailing list)
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
7fe5937b838b580eeaa061c85f700583?d=identicon&s=25 Commander Johnson (Guest)
on 2009-04-25 16:19
(Received via mailing list)
Indeed!

named_scope :only_published, :conditions => { :published => true }
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-04-25 16:58
(Received via mailing list)
On Apr 25, 3:18 pm, Commander Johnson <commanderjohn...@gmail.com>
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
7fe5937b838b580eeaa061c85f700583?d=identicon&s=25 Commander Johnson (Guest)
on 2009-04-28 19:20
(Received via mailing list)
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 <
2f8ec4a9ad7a39534f764d749e001046?d=identicon&s=25 Arzumy (Guest)
on 2009-06-23 10:45
(Received via mailing list)
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...@gmail.com>
This topic is locked and can not be replied to.