# ActiveRecord Error?

**URL:** <https://www.ruby-forum.com/t/activerecord-error/81616>\
**Category:** Rails\
**Created:** [January 10, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/activerecord-error/81616 "2007-01-10T17:39:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonlog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/db5fbb/32.png) [@jonlog](https://www.ruby-forum.com/u/jonlog)\
**Post date:** [January 10, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/activerecord-error/81616/1 "2007-01-10T17:39:49Z")

</div>

We spent a big chunk of yesterday hunting down a strange bug in one of  
our rails apps and created a test project to try out different theories  
on what was going on. The result so far is a very small test case app  
that perfectly reproduces the problem. The question is, is there  
anything _wrong_ with what we’re doing here, or is it a problem with  
ActiveRecord? We’re on rails 1.1.6 at the moment, so if this is a known  
bug that’s fixed in 1.2, just club me over the head with the trac  
reference.

## Given the following two models…

class Gadget \< ActiveRecord::Base  
has\_one :widget  
end

## class Widget \< ActiveRecord::Base belongs\_to :gadget def save gadget.save super end end

## and the following test…

class WidgetTest \< Test::Unit::TestCase  
fixtures :widgets, :gadgets

# Replace this with your real tests.

## def test\_update w = Widget.find(1) assert\_equal(w,w.gadget.widget) w.save end end

Obviously this is a contrived example (why would anyone every want to  
call w.gadget.widget?). However, it’s a greatly boiled down version of  
our problem app that has many models and a long series of methods  
calling each other from different models. Calling w.gadget.widget in  
this example doesn’t make any sense, however, to the best of my  
knowledge it’s not illegal either.

Given the above, if I comment out _either_ the ‘gadget.save’ line in  
widget.rb, _or_ the ‘… w.gadget.widget)’ assert line in the test,  
the test runs successfully. However, with both lines left in place, we  
get the following error from deep within the bowels of ActiveRecord.  
So, my question is, is there a Rails bug here, or are we doing something  
fundamentally inappropriate?

1. Error:  
test\_update(WidgetTest):  
ArgumentError: wrong number of arguments (1 for 0)

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/associations/association\_proxy.rb:110:in  
`save’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/associations/association\_proxy.rb:110:in  
`send’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/associations/association\_proxy.rb:110:in  
`method\_missing’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/callbacks.rb:349:in  
`callback’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/callbacks.rb:346:in  
`callback’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/callbacks.rb:341:in  
`each’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/callbacks.rb:341:in  
`callback’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/callbacks.rb:254:in  
`create\_or\_update’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/base.rb:1392:in  
`save\_without\_validation’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/validations.rb:736:in  
`save\_without\_transactions’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/transactions.rb:126:in  
`save’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/connection\_adapters/abstract/database\_statements.rb:51:in  
`transaction’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/transactions.rb:91:in  
`transaction’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/transactions.rb:118:in  
`transaction’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/transactions.rb:126:in  
`save’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/associations/association\_proxy.rb:110:in  
`send’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/associations/association\_proxy.rb:110:in  
`method\_missing’

/home/jgarvin/workspace/test\_case/config/…/app/models/widget.rb:5:in  
`save' ./test/unit/widget_test.rb:10:in`test\_update’

---

<div class="post-metadata">

**Author:** ![jonlog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/db5fbb/32.png) [@jonlog](https://www.ruby-forum.com/u/jonlog)\
**Post date:** [January 10, 2007, 7:33pm UTC](https://www.ruby-forum.com/t/activerecord-error/81616/2 "2007-01-10T19:33:22Z")

</div>

The most immediate problem is that you’ve overriden “save” but you  
haven’t matched the method signature of the superclass. Despite what the  
documentation says, “save” actually takes an argument (a boolean  
parameter that indicates whether validations are being performed).

If you change the method to look something like this:

def save(perform\_validations = true)  
gadget.save  
super  
end

or better yet, this:

def save(\*args)  
gadget.save  
super  
end

the problem should disappear.

I think a more fundamental problem might be that you’ve overriden the  
save method at all - I don’t think it is supposed to be overriden. Have  
you tried using one of the save hooks instead (before\_save, after\_save)?

---

<div class="post-metadata">

**Author:** ![jonlog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/db5fbb/32.png) [@jonlog](https://www.ruby-forum.com/u/jonlog)\
**Post date:** [January 10, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/activerecord-error/81616/3 "2007-01-10T19:40:10Z")

</div>

Hi

IMVHO is always better not to play redefining such method like save.  
Anyway I suspect sometime behind the scene the save method can be  
passed an argument.  
In your code the save method is defined that way

def save  
gadget.save  
super  
end

so it won’t accept any argument and will raise

ArgumentError: wrong number of arguments (1 for 0)

when 1 argument is passed.

I’d try to redefine the method this way

def save(\*args)  
gadget.save  
super(\*args)  
end

I’m not 100% sure and you may need some further inspection, but really  
smells like you’ve got this kind of problem.

Paolo

---

<div class="post-metadata">

**Author:** ![jonlog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/db5fbb/32.png) [@jonlog](https://www.ruby-forum.com/u/jonlog)\
**Post date:** [January 10, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/activerecord-error/81616/4 "2007-01-10T19:59:13Z")

</div>

HA! That’s it. adding (\*args) solved the problem instantly.

I agree that overriding the save was questionable, but there’s a rather  
complex series of things and conditions happening on save and at the  
time this code was written, that was what worked for us. It wasn’t  
until yesterday that some other code caused this code to break, and our  
wonderful tests identified the problem immediately, long before we  
deployed the revision. 😉 Hooray for tests!
