jwood / tenacity

A database client independent way of managing relationships between models backed by different databases.
MIT License
118 stars 17 forks source link

Save bug fix #37

Closed raphaelcm closed 12 years ago

raphaelcm commented 12 years ago

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:

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:

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:

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.

jwood commented 12 years ago

This looks great Raphael, thanks! I'll get this merged in soon.

raphaelcm commented 12 years ago

No problem, thanks for making/maintaining Tenacity. My company is going with "polyglot persistence" in a big way and this has been a huge help.

Sent from my iPhone

On Apr 23, 2012, at 6:03 PM, John Woodreply@reply.github.com wrote:

This looks great Raphael, thanks! I'll get this merged in soon.


Reply to this email directly or view it on GitHub: https://github.com/jwood/tenacity/pull/37#issuecomment-5293069

jwood commented 12 years ago

Fixed in version 0.5.6