# Applying conditions to ActiveRecord call in model.rb

**URL:** <https://www.ruby-forum.com/t/applying-conditions-to-activerecord-call-in-model-rb/100980>\
**Category:** Rails\
**Created:** [August 2, 2006, 7:52am UTC](https://www.ruby-forum.com/t/applying-conditions-to-activerecord-call-in-model-rb/100980 "2006-08-02T07:52:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [August 2, 2006, 7:52am UTC](https://www.ruby-forum.com/t/applying-conditions-to-activerecord-call-in-model-rb/100980/1 "2006-08-02T07:52:27Z")

</div>

If @c is an instance for the User class and lets say I want to call  
@c.pictures, which would be the user’s pictures.

How can I setup up RoR so that I could append the condition  
“status=‘available’” to the conditions arguments EVERYTIME .pictures is  
called. This way I can only call available pictures

Is there some kind of filter I can apply to the picture.rb or my  
user.rb?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [August 2, 2006, 11:15am UTC](https://www.ruby-forum.com/t/applying-conditions-to-activerecord-call-in-model-rb/100980/2 "2006-08-02T11:15:50Z")

</div>

Eric G. wrote:

> If @c is an instance for the User class and lets say I want to call  
> @c.pictures, which would be the user’s pictures.
> 
> How can I setup up RoR so that I could append the condition  
> “status=‘available’” to the conditions arguments EVERYTIME .pictures is  
> called. This way I can only call available pictures

class User  
has\_many :pictures, :conditions =\> [‘status = ?’, ‘available’]  
end

Not sure if the SQL is called again and again or if the values are  
cached after the first call. Let me know.

Cheers  
Nic

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [August 2, 2006, 11:13pm UTC](https://www.ruby-forum.com/t/applying-conditions-to-activerecord-call-in-model-rb/100980/3 "2006-08-02T23:13:01Z")

</div>

Dr Nic,

I dont really know how to tell if they are cached or not, but your  
advice solved my problem. Thanks a lot.

Eric

Dr Nic wrote:

> Eric G. wrote:
> 
> > If @c is an instance for the User class and lets say I want to call  
> > @c.pictures, which would be the user’s pictures.
> > 
> > How can I setup up RoR so that I could append the condition  
> > “status=‘available’” to the conditions arguments EVERYTIME .pictures is  
> > called. This way I can only call available pictures
> 
> class User  
> has\_many :pictures, :conditions =\> [‘status = ?’, ‘available’]  
> end
> 
> Not sure if the SQL is called again and again or if the values are  
> cached after the first call. Let me know.
> 
> Cheers  
> Nic

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [August 3, 2006, 12:47am UTC](https://www.ruby-forum.com/t/applying-conditions-to-activerecord-call-in-model-rb/100980/4 "2006-08-03T00:47:58Z")

</div>

Eric G. wrote:

> I dont really know how to tell if they are cached or not, but your  
> advice solved my problem. Thanks a lot.

You can tell if its cached by watching the log. Assuming you are  
running in development, open a shell window, cd to your project  
directory, and type “tail -f log/development.log”. Whenever you do a  
query you’ll see the SQL logged to that file. If an association is  
returning cached results, you won’t see a new query in the log. If you  
need to force a query you can either do user.pictures(true) or  
user.pictures.reload. Usually caching is a good thing.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [August 3, 2006, 12:06pm UTC](https://www.ruby-forum.com/t/applying-conditions-to-activerecord-call-in-model-rb/100980/5 "2006-08-03T12:06:39Z")

</div>

class User \< ActiveRecord::Base  
has\_many :pictures, :conditions =\> ‘status = “available”’  
end

-Jonathan.
