Make your Active Records “paranoid.” Deleting them does not delete the row, but set a deleted_at field. Find is overloaded to skip deleted records.
http://ar-paranoid.rubyforge.org/
Update: April 29, 2007
Tested with Rails 1.2.3:
The base gem, acts_as_paranoid (0.3.1), breaks for all rails versions higher than 1.1.6.
You need to manually install it as a plugin.
Put this in your vendors/plugins directory:
http://svn.techno-weenie.net/projects/plugins/plugins/acts_as_paranoid/
If you install acts_as_paranoid as a plugin inside your app you don’t need to require ‘acts_as_paranoid’ in neither environment.rb nor application.rb
And inside environment.rb:
require ‘acts_as_paranoid’
Previously:
The documentation for this is pretty sparse, so I’ve added some of my own experiences my blog: some weird bugs, overriding associations etc.
See also:
Update: Apr 25, 2006
I would like to add the following that the existing version doesn’t work with Rails 1.1, due to the assumption on the part that scoped_methods is not an array (scope up to one level of nesting only). With the added support to nested scope in Rails 1.1, the part of the code where it tries to work around the scope method no longer works.
I’ve created a simple work-around to this problem, and i’ve outlined it on my website. I thought you might be interested to take a look.
site no longer works – April 29, 2007
Update: May 18th 2006
With PostgreSQL (and Oracle) an index on a nullable column (such as deleted_at) will NOT be used for queries that have “deleted_at IS NULL‿ in the where clause. To have PostgreSQL use an index, see this discussion.
Update June 13th 2006:
I’ve been using the latest acts_as_paranoid with Rails 1.1.2 and it works without any hacks… But get it from here:
script/plugin source http://svn.techno-weenie.net/projects/plugins
script/plugin install acts_as_paranoid
See also Plugins
Not working with out-of-the-box Rails 1.1.6 install on linux. Haven’t tried with windows. In either case, I’ve encountered the exact same problem as the guy below. I also tried the updated acts_as_paranoid.rb gem fix in my project’s lib directory.
can’t work
aotianlong@aotianlong:/workspace/rails/bar$ ruby script/server -p3001
=> Booting WEBrick…
script/../config/../vendor/plugins/acts_as_paranoid/init.rb:14:in `load_plugin’: undefined method `alias_method_chain’ for #
aotianlong@aotianlong:
Update October 31, 2006
Now working with 1.1.6, get the new source here:
http://svn.techno-weenie.net/projects/plugins/tags/acts_as_paranoid-1.1.4/
I had luck just doing an svn co on that url into my vendor/plugins directory. Using svn export will pull the code down without .svn directories, which is probably what you want.
Make your Active Records “paranoid.” Deleting them does not delete the row, but set a deleted_at field. Find is overloaded to skip deleted records.
http://ar-paranoid.rubyforge.org/
Update: April 29, 2007
Tested with Rails 1.2.3:
The base gem, acts_as_paranoid (0.3.1), breaks for all rails versions higher than 1.1.6.
You need to manually install it as a plugin.
Put this in your vendors/plugins directory:
http://svn.techno-weenie.net/projects/plugins/plugins/acts_as_paranoid/
If you install acts_as_paranoid as a plugin inside your app you don’t need to require ‘acts_as_paranoid’ in neither environment.rb nor application.rb
And inside environment.rb:
require ‘acts_as_paranoid’
Previously:
The documentation for this is pretty sparse, so I’ve added some of my own experiences my blog: some weird bugs, overriding associations etc.
See also:
Update: Apr 25, 2006
I would like to add the following that the existing version doesn’t work with Rails 1.1, due to the assumption on the part that scoped_methods is not an array (scope up to one level of nesting only). With the added support to nested scope in Rails 1.1, the part of the code where it tries to work around the scope method no longer works.
I’ve created a simple work-around to this problem, and i’ve outlined it on my website. I thought you might be interested to take a look.
site no longer works – April 29, 2007
Update: May 18th 2006
With PostgreSQL (and Oracle) an index on a nullable column (such as deleted_at) will NOT be used for queries that have “deleted_at IS NULL‿ in the where clause. To have PostgreSQL use an index, see this discussion.
Update June 13th 2006:
I’ve been using the latest acts_as_paranoid with Rails 1.1.2 and it works without any hacks… But get it from here:
script/plugin source http://svn.techno-weenie.net/projects/plugins
script/plugin install acts_as_paranoid
See also Plugins
Not working with out-of-the-box Rails 1.1.6 install on linux. Haven’t tried with windows. In either case, I’ve encountered the exact same problem as the guy below. I also tried the updated acts_as_paranoid.rb gem fix in my project’s lib directory.
can’t work
aotianlong@aotianlong:/workspace/rails/bar$ ruby script/server -p3001
=> Booting WEBrick…
script/../config/../vendor/plugins/acts_as_paranoid/init.rb:14:in `load_plugin’: undefined method `alias_method_chain’ for #
aotianlong@aotianlong:
Update October 31, 2006
Now working with 1.1.6, get the new source here:
http://svn.techno-weenie.net/projects/plugins/tags/acts_as_paranoid-1.1.4/
I had luck just doing an svn co on that url into my vendor/plugins directory. Using svn export will pull the code down without .svn directories, which is probably what you want.