airblade/paper_trail — Issue #128: Unable to subclass Version and specify alternate table name
Author: wgrrrr
Created: 2012-01-25 01:11:29+00:00
State: open

I think I've found a bug related to creating your own Version class (subclassing Version) and altering the table name. My app has individual version tables for each of the models. It recently broke when I upgraded to Rails 3.2. In an effort to track this down, I performed the following steps:

1. Create new rails app with ```rails new testapp```
2. Add the paper_trail gem to the Gemfile
3. Create a test User model with ```rails g model user```
3. Install paper_trail with ```bundle exec rails generate paper_trail:install```
4. Customize the generated paper_trail migration to call the table ```user_versions```
5. Edit the User class to add ```has_paper_trail :class => 'UserVersion'```
6. Create the UserVersion model as:
```
class UserVersion < Version
self.table_name = "user_versions"
end
```
7. Run the migrations with ```rake db:migrate```
8. Enter Rails console and attempt to create a User object.

I get the following exception:
```
>> User.create
(0.1ms) begin transaction
SQL (15.6ms) INSERT INTO "users" ("created_at", "updated_at") VALUES (?, ?) [["created_at", Wed, 25 Jan 2012 01:00:01 UTC +00:00], ["updated_at", Wed, 25 Jan 2012 01:00:01 UTC +00:00]]
(2.3ms) rollback transaction
ActiveRecord::StatementInvalid: Could not find table 'versions'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/connection_adapters/sqlite_adapter.rb:470:in `table_structure'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/connection_adapters/sqlite_adapter.rb:351:in `columns'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/connection_adapters/schema_cache.rb:12:in `block in initialize'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/model_schema.rb:228:in `yield'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/model_schema.rb:228:in `default'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/model_schema.rb:228:in `columns'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/model_schema.rb:248:in `column_names'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/attribute_methods.rb:47:in `block in define_attribute_methods'
from :10:in `synchronize'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/attribute_methods.rb:44:in `define_attribute_methods'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/attribute_methods.rb:46:in `block in define_attribute_methods'
from :10:in `synchronize'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/attribute_methods.rb:44:in `define_attribute_methods'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/attribute_methods.rb:148:in `respond_to?'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/attribute_assignment.rb:81:in `block in assign_attributes'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/attribute_assignment.rb:78:in `each'
... 28 levels...
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/attribute_methods/dirty.rb:22:in `save'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/transactions.rb:241:in `block (2 levels) in save'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:190:in `transaction'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/transactions.rb:208:in `transaction'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/transactions.rb:241:in `block in save'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/transactions.rb:240:in `save'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/activerecord-3.2.0/lib/active_record/persistence.rb:45:in `create'
from (irb):1
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/railties-3.2.0/lib/rails/commands/console.rb:47:in `start'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/railties-3.2.0/lib/rails/commands/console.rb:8:in `start'
from /Users/x/.rvm/gems/ruby-1.9.2-p290@app/gems/railties-3.2.0/lib/rails/commands.rb:41:in `'
```

Before running this test I completely emptied my gemset and redownloaded everything - everything being only the gems used in this test.

Any ideas? :-)