Ruby Forum Ruby on Rails > prevent caching

Posted by Luma (Guest)
on 08.03.2008 22:30
(Received via mailing list)
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
Posted by Frederick Cheung (Guest)
on 09.03.2008 14:24
(Received via mailing list)
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