jwood/tenacity — pull request #37 by raphaelcm (2012-04-23 15:23:02+00:00)
Title: Save bug fix

Makes a couple tweaks so that tests can run "out of the box" on vanilla systems.

Also fixes two bugs:

**Bug 1**

Given the following setup:

```ruby
class ActiveRecordUser < ActiveRecord::Base
include Tenacity

belongs_to :active_record_organization, :autosave => true
end

class ActiveRecordOrganization < ActiveRecord::Base
include Tenacity

has_many :active_record_users
end

class MongoidCampusHub
include Mongoid::Document
include Tenacity

t_belongs_to :active_record_organization

end
```

This scenario throws an error:

```ruby
org = ActiveRecordOrganization.create
campus_hub = MongoidCampusHub.create
campus_hub.active_record_organization = org
campus_hub.save!
user = ActiveRecordUser.new
user.active_record_organization = campus_hub.active_record_organization
user.save
```

Here's the error:

```
ArgumentError: wrong number of arguments (1 for 0)
from /var/bundler/turtle/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/associations/association_proxy.rb:225:in `save'
from /var/bundler/turtle/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/associations/association_proxy.rb:225:in `send'
from /var/bundler/turtle/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/associations/association_proxy.rb:225:in `method_missing'
from /var/bundler/turtle/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/autosave_association.rb:360:in `save_belongs_to_association'
from /var/bundler/turtle/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/autosave_association.rb:172:in `autosave_associated_records_for_organization'
from /var/bundler/turtle/ruby/1.8/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:420:in `_run_save_callbacks'
from /var/bundler/turtle/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/callbacks.rb:273:in `create_or_update'
from /var/bundler/turtle/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/persistence.rb:39:in `save'
from /var/bundler/turtle/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/validations.rb:43:in `save'
from /var/bundler/turtle/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/attribute_methods/dirty.rb:21:in `save'
from /var/bundler/turtle/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/transactions.rb:240:in `save'
from /var/bundler/turtle/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/transactions.rb:292:in `with_transaction_returning_status'
from /var/bundler/turtle/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
from /var/bundler/turtle/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/transactions.rb:207:in `transaction'
from /var/bundler/turtle/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/transactions.rb:290:in `with_transaction_returning_status'
from /var/bundler/turtle/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/transactions.rb:240:in `save'
from /var/bundler/turtle/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/transactions.rb:251:in `rollback_active_record_state!'
from /var/bundler/turtle/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/transactions.rb:239:in `save'
```

It's caused because the save method in `associate_proxy.rb` doesn't accept any arguments, such as `:validate => false`, which gets automatically sent by ActiveRecord.

**Bug 2**

If an object is unchanged, save returns false. This causes problems in typical controller update methods:

```ruby
def update
@my_obj.update_attributes(params[:my_obj_attributes])

if @my_obj.save
render :index
else
render :edit
end
end
```

If the user visits the edit form and clicks 'update' without making any changes, the controller will render the edit form again, instead of going to the `:index` action, as it should.