# Prevent caching

**URL:** <https://www.ruby-forum.com/t/prevent-caching/131369>\
**Category:** Rails\
**Created:** [March 8, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/prevent-caching/131369 "2008-03-08T22:30:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d6d6ee/32.png) [@luma](https://www.ruby-forum.com/u/luma)\
**Post date:** [March 8, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/prevent-caching/131369/1 "2008-03-08T22:30:21Z")

</div>

hi

I’m trying to validate foreign keys:

class Blocking \< ActiveRecord::Base

belongs\_to :ressource

validates\_inclusion\_of :ressource\_id, :in =\>  
Ressource.select\_all\_ids

end

class Ressource \< ActiveRecord::Base

has\_many :blockings, :dependent =\> :destroy

def self.select\_all\_ids  
self.find(:all, :select =\> “id”).map {|ressource| ressource.id}  
end

end

I’ve created a new ressource. Now, I’d like to create a blocking that  
belongs to this ressource. But validation fails because the result of  
the method select\_add\_ids doesn’t include the id of the new ressource.  
The method seems to be cached.

What kind of caching is this, and how can I prevent the method from  
being cached?  
(I use mysql 5.0)

thanks

Luma

---

<div class="post-metadata">

**Author:** ![luma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d6d6ee/32.png) [@luma](https://www.ruby-forum.com/u/luma)\
**Post date:** [March 9, 2008, 2:24pm UTC](https://www.ruby-forum.com/t/prevent-caching/131369/2 "2008-03-09T14:24:14Z")

</div>

On 8 Mar 2008, at 21:29, Luma wrote:

> the method select\_add\_ids doesn’t include the id of the new ressource.  
> The method seems to be cached.
> 
> What kind of caching is this, and how can I prevent the method from  
> being cached?

This is not really caching as such, just that i suspect you haven’t  
fully understood what code is executed when.  
In particular, the validates\_inclusion\_of… statement is executed  
when blocking.rb is loaded (and in production this will happen exactly  
onece) and so the select\_all\_ids is executed at that time (not when a  
record is checked).  
You could keep this approach using a validates method, but really this  
is the sort of thing that I’d let the database enforce (via a foreign  
key).

Fred
