bjones/safe_attributes — Issue #4: Error saving new model with "attribute" attribute
Author: kmatthews812
Created: 2011-12-09 05:04:37+00:00
State: open

I think I found 1 case where your gem fails. I can read and update models with an attribute column named "attribute", but I cannot save a new one when the attribute name is "attribute". Other attribute names work fine for all methods.

Try this with Ruby 1.9.2 and Rails 3.1.3

> rails g model TestModel

CreateTestModels.rb

```ruby
class CreateTestModels < ActiveRecord::Migration
def change
create_table :test_models do |t|
t.string :attribute
t.timestamps
end
end
end
```

TestModel.rb

```ruby
class TestModel < ActiveRecord::Base
bad_attribute_names :attribute

def blah= value
self[:attribute] = value
end

def blah
self[:attribute]
end

end
```

> rails console
Loading development environment (Rails 3.1.3)
ruby-1.9.2-p290 :001 > t = TestModel.new
=> #
ruby-1.9.2-p290 :002 > t.blah = 'blah'
=> "blah"
ruby-1.9.2-p290 :003 > t.save
(512.0ms) BEGIN
SQL (295.2ms) INSERT INTO `test_models` (`attribute`, `created_at`, `updated_at`) VALUES ('blah', '2011-12-09 06:00:21', '2011-12-09 06:00:21')
(305.7ms) ROLLBACK
ArgumentError: wrong number of arguments (0 for 1)
from /Users/kmatthews812/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/attribute_methods/read.rb:141:in `attribute'
from /Users/kmatthews812/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.1.3/lib/active_model/dirty.rb:143:in `attribute_change'
from /Users/kmatthews812/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.1.3/lib/active_model/dirty.rb:117:in `block in changes'

The strange part is that the data is actually saved to the database. The error is thrown after the data is saved and the rollback never occurs. Any ideas on how to fix this? I have no ability to change the database column name, as this is a legacy database.

Thanks!