having a default condition in model

Is there a way to specify at the model level that you want all calls to
find(), find_by_xxxx(), etc, to include a certain :conditions clause?

for instance, if normally you wanted to only select articles
where "is_published = 1" (which you could of course override in admin
mode or something)

because with this flag, when you are selecting articles from different
views and/or controllers, it requires them to have knowledge of the
underlying articles model and even the DB table. Doesn't seem very DRY.

And in fact we wanted to add another default flag, "is_active = 0"
(because deleted articles are really just flagged, not deleted). So now
having to go through the whole app....

thanks
Sam

Find with scope, then alias' the find methods. There is an example of
this in acts_as_versioned by Rick Olson

Zach Inglis
→ Blog — http://www.zachinglis.com
→ Company — http://www.lt3media.com
→ Portfolio — http://portfolio.zachinglis.com

Sam,

You're asking two different questions actually :

   > Is there a way to specify at the model level that you want all calls to
   > find(), find_by_xxxx(), etc, to include a certain :conditions clause?

The plugin ScopedAccess
  http://agilewebdevelopment.com/plugins/scoped_access
does just that. You may look at the code to see how it works.

   > for instance, if normally you wanted to only select articles
   > where "is_published = 1"

That's a different case, and much easier to satisfy thanks to Rails
associations.

Ex:
  class User << AR
      has_many :articles
      has_many :published_articles , :conditions => "is_published = 1" ,
                   :class_name => 'Article', :foreign_key => 'article_id'
  end

Alain Ravet

Thanks for the quick response!

Sam,

You're asking two different questions actually :

   > Is there a way to specify at the model level that you want all

calls to

   > find(), find_by_xxxx(), etc, to include a certain :conditions

clause?

The plugin ScopedAccess
  http://agilewebdevelopment.com/plugins/scoped_access
does just that. You may look at the code to see how it works.

ok that looks cool - but according to a comment at the bottom of that
page "This has been incorporated into Rails" - is that true? The
homepage for it gives a 404 and there seems to be some red flags about
it in other posts... hmm. Another scary plugin?
http://www.mail-archive.com/rubyonrails-core@googlegroups.com/msg01489.html

[another topic: Is there anything in Rails equivalent to CPAN or PEAR
where you can reasonably be sure the code is stable and high-quality?
I've already gone down the road with a few plugins that were broken or
deprecated or at least early beta]

   > for instance, if normally you wanted to only select articles
   > where "is_published = 1"

That's a different case, and much easier to satisfy thanks to Rails
associations.

Ex:
  class User << AR
      has_many :articles
      has_many :published_articles , :conditions => "is_published

= 1" ,

                   :class_name => 'Article', :foreign_key

=> 'article_id'

  end

ok I was aware of this option but how would I over-ride it if the
current user was an admin for instance? Do I have to create a separate
association for every combination of flags then, and call different
methods based on the role of the current_user? (say I have
is_published, is_deleted, is_public_domain - that's 9 potential
associations, yux)

Thanks
Sam