# Having a default condition in model

**URL:** <https://www.ruby-forum.com/t/having-a-default-condition-in-model/104344>\
**Category:** Rails\
**Created:** [May 16, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/having-a-default-condition-in-model/104344 "2007-05-16T23:37:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbeam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@sbeam](https://www.ruby-forum.com/u/sbeam)\
**Post date:** [May 16, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/having-a-default-condition-in-model/104344/1 "2007-05-16T23:37:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sbeam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@sbeam](https://www.ruby-forum.com/u/sbeam)\
**Post date:** [May 16, 2007, 11:55pm UTC](https://www.ruby-forum.com/t/having-a-default-condition-in-model/104344/2 "2007-05-16T23:55:29Z")

</div>

Find with scope, then alias’ the find methods. There is an example of  
this in acts\_as\_versioned by Rick O.

Zach I.  
â†’ Blog â€” [http://www.zachinglis.com](http://www.zachinglis.com)  
â†’ Company â€” [http://www.lt3media.com](http://www.lt3media.com)  
â†’ Portfolio â€” [http://portfolio.zachinglis.com](http://portfolio.zachinglis.com)

---

<div class="post-metadata">

**Author:** ![sbeam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@sbeam](https://www.ruby-forum.com/u/sbeam)\
**Post date:** [May 16, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/having-a-default-condition-in-model/104344/3 "2007-05-16T23:58:29Z")

</div>

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](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 R.

[http://blog.ravet.com](http://blog.ravet.com)

---

<div class="post-metadata">

**Author:** ![sbeam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@sbeam](https://www.ruby-forum.com/u/sbeam)\
**Post date:** [May 17, 2007, 2:12am UTC](https://www.ruby-forum.com/t/having-a-default-condition-in-model/104344/4 "2007-05-17T02:12:39Z")

</div>

Thanks for the quick response!

On Wednesday 16 May 2007, Alain R. 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](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/removed\_email\_address@domain.invalid/msg01489.html](http://www.mail-archive.com/removed_email_address@domain.invalid/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
