Forum: Ruby on Rails having a default condition in model

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.
56eb877c42e47774db729bcfb8cad46f?d=identicon&s=25 sbeam (Guest)
on 2007-05-16 23:37
(Received via mailing list)
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
Df4978da52459529e539da4c995918cb?d=identicon&s=25 Zach Inglis // LT3media (Guest)
on 2007-05-16 23:55
(Received via mailing list)
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
Ba3a00606eb530dcab2c4a6a59bf366d?d=identicon&s=25 Alain Ravet (Guest)
on 2007-05-16 23:58
(Received via mailing list)
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
----
http://blog.ravet.com
56eb877c42e47774db729bcfb8cad46f?d=identicon&s=25 sbeam (Guest)
on 2007-05-17 02:12
(Received via mailing list)
Thanks for the quick response!

On Wednesday 16 May 2007, Alain Ravet wrote:
>
> 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@googl...

[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
This topic is locked and can not be replied to.