Prevent caching

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

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