Dave
2006-May-11 10:14 UTC
[Rails] acts_as_paranoid, aliasing and nested scopes in Rails 1.1
I have a real problem with the acts_as_paranoid plugin. The aliasing of
find and so the nested scope does not seem to work at all, when
combining it with similar plugins.
The description:
I made a copy of ''acts_as_paranoid'' in the plugin directory,
renamed it
to ''acts_as_very_new''(just a nonsense plugin for testing), and
simplified the code (original code of acts_as_paranoid can be found
here:
http://svn.techno-weenie.net/projects/plugins/acts_as_paranoid/lib/caboose/acts/paranoid.rb):
#--------------------------------------------------------------BEGIN
module VeryNew
#...
module ClassMethods
def acts_as_very_new
unless is_very_new?
class << self
alias_method :find_with_old, :find
end
end
include InstanceMethods
end
#...
end
module InstanceMethods #:nodoc:
#...
module ClassMethods
def find(*args)
with_very_new_scope { find_with_old(*args) }
end
protected
def with_very_new_scope(&block)
with_scope({:find => {:conditions =>
["#{table_name}.updated_at > ?", (Time.new - 3.days)]} }, :merge,
&block)
end
end
end
end
#--------------------------------------------------------------END
As you can see, there''s no change in structure, it''s the same
as the
original ''acts_as_paranoid'' only with a different
find-condition. Next I
added following code to a model:
#--------------------------------------------------------------BEGIN
class Book < ActiveRecord::Base
acts_as_paranoid
acts_as_very_new
#...
end
#--------------------------------------------------------------END
In a controller I did the following calls:
Book.find(:all)
# SQL: SELECT * FROM books WHERE ((books.updated_at > ''2006-05-08
11:38:33'') AND (books.deleted_at IS NULL))
Book.find_with_old(:all)
# SQL: SELECT * FROM books WHERE (books.deleted_at IS NULL)
Book.find_with_deleted(:all)
# SQL: SELECT * FROM books
Isn''t is strange? Shouldn''t
"Book.find_with_deleted(:all)" generate a
"WHERE (books.updated_at > ''2006-05-08
11:38:33'')" condition? The
problem seems to be in aliasing of find. When I switch the call order of
"acts_as_paranoid" and "acts_as_very_new" in the model, I
get:
Book.find(:all)
# SQL: SELECT * FROM books WHERE ((books.updated_at > ''2006-05-08
11:38:33'') AND (books.deleted_at IS NULL))
Book.find_with_old(:all)
# SQL: SELECT * FROM books
Book.find_with_deleted(:all)
# SQL: SELECT * FROM books WHERE (books.updated_at > ''2006-05-08
11:38:33'')
Has anyone an idea how to fix it?
Thanks in advance,
Dave
--
Posted via http://www.ruby-forum.com/.
