Persistent error - wrong number of arguments (1 for 0)

Hello all,

I'm a bit of a Nooby on Rails (is that a new joke?) as well as a Ruby
Nooby and I've been trying all day to get to the bottom of an error. I
promise I have tried everything but I've started going round in circles
and deleting large chunks of code so it's probably time to call in the
experts.

I've seen a few similar posts to this, mostly on this forum, but none
seem to be entirely applicable to my case.

I have a formtastic form which gathers some inputs and creates a new
Transaction which then generates a nasty error. it seems to be centred
on the @transaction.save action, this is where my 'puts' debugging
outputs stop.

I've done nothing (intentionally) advanced, mainly relying on defaults
and nifty_generators, so I can't see how I've got myself into such a
pickle.

Any help you can offer would be very much appreciated, please let me
know if I've missed something you need to see. I really am going
insane.

Kind regards

Sam

[transactions_controller.rb in part]
--------------- --------

  def new
    @transaction = Transaction.new
  end

  def create
     @transaction = Transaction.new(params[:transaction])
      if @transaction.save <<< this is line 36
         flash[:notice] = "Successfully created transaction."
         redirect_to items_path
       else
          render :action => 'new'
       end
   end

See http://wiki.rubyonrails.org/rails/pages/ReservedWords
(particularly words starting with Tra...)

Colin

Colin Law wrote:

words starting with Tra...)

Colin

Oh no! Wow, it's going to be fun to unpick all that... transaction is
probably the most used word in my entire project!

Thanks Colin, at least I can direct my efforts into fixing it rather
than pulling my hair out. You're a life saver.

Sam

...and here was me hoping someone would say "you have the wrong brackets
on line 12"...

Sam Ward wrote:

Colin Law wrote:

words starting with Tra...)

Colin

Oh no! Wow, it's going to be fun to unpick all that... transaction is
probably the most used word in my entire project!

Well, this is where your automated tests come in handy:

1. Change name of model Transaction to something else.
2. Run tests, find where they complain about "undefined constant
Transaction" or something.
3. Fix those spots.

Thanks Colin, at least I can direct my efforts into fixing it rather
than pulling my hair out. You're a life saver.

Sam

Best,

Colin Law wrote:

words starting with Tra...)

Colin

Oh no! Wow, it's going to be fun to unpick all that... transaction is
probably the most used word in my entire project!

It might not be that bad, you should just (?) be able to change the
file names and do a global search and replace of transaction to
newname and Transaction to Newname. Oh and change the table name of
course via a migration. Then as Marnen has pointed out the tests
should sort out anything forgotten.

That assumes you have not used any 'transactions' of course (which is
why it is reserved).

Make sure you only do the global change in your code, if you have any
plugins or rails itself vendored then it would not be good to change
it there.

Good luck

Colin

Sam Ward wrote:

Colin Law wrote:

words starting with Tra...)

Colin

Oh no! Wow, it's going to be fun to unpick all that... transaction is
probably the most used word in my entire project!

Thanks Colin, at least I can direct my efforts into fixing it rather
than pulling my hair out. You're a life saver.

Sam

...and here was me hoping someone would say "you have the wrong brackets
on line 12"...

There is one other alternative, and I hesitate to mention it because
you're still asking for long-term maintenance issues (i.e., Marnen and
Colin have the right of it - change your code)...

but you *can* preface your Transaction model references with '::'
whenever utilizing a method from your Transaction class, such as:

  def new
    @transaction = ::Transaction.new
  end

  def create
    @transaction = ::Transaction.new(params[:transaction])
    if @transaction.save
      flash[:notice] = "Successfully created transaction."
      redirect_to items_path
    else
      render :action => 'new'
    end
  end

The plot thickens....

Thank you guys for you help and how quickly you got back to me. I have
now carefully searched and replaced "Transaction" with "Loan" throughout
my project and, believe it or not, I still have the same error!!

The Rails Gods are harsh masters.

Any other ideas? Have I broken something? I am totally flummoxed.

Rapidly Balding Sam

Here's my stack trace for good measure:
ArgumentError in LoansController#create

wrong number of arguments (1 for 0)
RAILS_ROOT: /Users/sam/Sites/myunibox

Application Trace | Framework Trace | Full Trace
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/quoting.rb:61:in
`to_s'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/quoting.rb:61:in
`quoted_date'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/quoting.rb:29:in
`quote'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/mysql_adapter.rb:236:in
`quote'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:3033:in
`attributes_with_quotes'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:3024:in
`each'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:3024:in
`attributes_with_quotes'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2939:in
`create_without_timestamps'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/timestamp.rb:53:in
`create_without_callbacks'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/callbacks.rb:266:in
`create'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2915:in
`create_or_update_without_callbacks'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/callbacks.rb:250:in
`create_or_update'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2573:in
`save_without_validation'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:1090:in
`save_without_dirty'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/dirty.rb:79:in
`save_without_transactions'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:229:in
`send'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:229:in
`with_transaction_returning_status'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in
`transaction'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:182:in
`transaction'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:228:in
`with_transaction_returning_status'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:196:in
`save'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:208:in
`rollback_active_record_state!'
/Users/sam/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:196:in
`save'
/Users/sam/Sites/boxapp/app/controllers/loans_controller.rb:36:in
`create'

Sam Ward wrote:

The plot thickens....

Thank you guys for you help and how quickly you got back to me. I have
now carefully searched and replaced "Transaction" with "Loan" throughout
my project and, believe it or not, I still have the same error!!

Which tests are failing? Are any tests now passing that were failing,
or vice versa?

The Rails Gods are harsh masters.

Any other ideas? Have I broken something? I am totally flummoxed.

You've probably missed something: your stack trace still seems to
involve Rails' transaction code, which probably means you're still using
the reserved word "transaction" somewhere. (Rails uses that for
database transactions.)

Rapidly Balding Sam

Best,

Sam Ward wrote:

Rapidly Balding Sam

A good razor in the shower will take care of those worries...

Here's my stack trace for good measure:
ArgumentError in LoansController#create

Can we see your current LoansController code for new and create?

The model definition would probably help as well.

The plot thickens....

Thank you guys for you help and how quickly you got back to me. I have
now carefully searched and replaced "Transaction" with "Loan" throughout
my project and, believe it or not, I still have the same error!!

The Rails Gods are harsh masters.

Any other ideas? Have I broken something? I am totally flummoxed.

It's calling to_s on something, trying to pass it the argument :db
(rails extends to_s on time and a few other classes), but the object
in question obviously isn't expecting to have to_s called on it in
that way. I'd stick a breakpoint in there to see what exactly rails is
calling to_s on (but at a guess it could be because you're supplying
something that isn't a time or a date somehere where rails is
expecting one)

Fred

Which tests are failing? Are any tests now passing that were failing,

or vice versa?

I've not done any tests before or since, I got a bit carried away before
I got to the chapter on tests in my book (is that bad of me?). I'm
literally just trying to use the site and picking out errors by hand,
this one is quite obvious as being able to save a Loan basically the
whole point of my site!

You've probably missed something: your stack trace still seems to

involve Rails' transaction code
If I do a project wide case-insensitive search on all files within the
appname directory created by "rails appname", the only instance of
'transaction' is found in this file:

test_helper.rb
self.use_transactional_fixtures = true

I left this alone because I didn't really understand what it was.

Can we see your current LoansController code for new and create?

  def new
    @loan = Loan.new
  end

  def create
     @loan = Loan.new(params[:loan])

       if @loan.save <<< line 36 (this is not in my code, I just put it
here)
         flash[:notice] = "Successfully created loan."
         redirect_to items_path
        else
          render :action => 'new'
        end
   end

The model definition would probably help as well.

class Loan < ActiveRecord::Base
  #attr_accessible :item_id, :user_id, :from, :to, :status

  belongs_to :item
  belongs_to :user

  def owner
    @item = Item.find(item_id)
    User.find(@item.user_id)
  end

  def borrower
    User.find(user_id)
  end
end

-- ends

Thanks again for your time on this, I am the only person I know using
Ruby and/or Rails, I would be totally adrift without you guys.

Sam

Sam Ward wrote:

>Which tests are failing? Are any tests now passing that were failing,
or vice versa?

I've not done any tests before or since, I got a bit carried away before
I got to the chapter on tests in my book (is that bad of me?).

Yes. You've now discovered why you should do all development
test-first: it makes it much easier to determine when your app is
actually failing, and where your undefined constants are.

So...stop now. Install Autotest, RSpec, Cucumber, and Machinist, and
write comprehensive tests for your application. Don't write another
line of app code till all your tests pass (except what's necessary to
fix existing bugs, of course). Do all further development test-first --
that is, write the tests *before* writing the code that will make them
pass. You'll be a lot happier.

I'm
literally just trying to use the site and picking out errors by hand,
this one is quite obvious as being able to save a Loan basically the
whole point of my site!

You've probably missed something: your stack trace still seems to

involve Rails' transaction code
If I do a project wide case-insensitive search on all files within the
appname directory created by "rails appname", the only instance of
'transaction' is found in this file:

test_helper.rb
self.use_transactional_fixtures = true

I left this alone because I didn't really understand what it was.

That's fine; it should stay as it is. (And the rdoc should tell you
what that is.)

Can we see your current LoansController code for new and create?

  def new
    @loan = Loan.new
  end

  def create
     @loan = Loan.new(params[:loan])

       if @loan.save <<< line 36 (this is not in my code, I just put it
here)
         flash[:notice] = "Successfully created loan."
         redirect_to items_path
        else
          render :action => 'new'
        end
   end

This looks fine.

The model definition would probably help as well.

class Loan < ActiveRecord::Base
  #attr_accessible :item_id, :user_id, :from, :to, :status

Did you mean to comment that line out? (It's probably OK to have done
so; just asking.)

  belongs_to :item
  belongs_to :user

  def owner
    @item = Item.find(item_id)
    User.find(@item.user_id)
  end

That's a pretty bad way of doing things if I understand correctly what
you're trying to do. You shouldn't need that method at all.

  def borrower
    User.find(user_id)
  end

Again, unnecessary. Rails' associations will take care of that
automatically.

end

-- ends

Thanks again for your time on this, I am the only person I know using
Ruby and/or Rails,

Then join a local user group if you can!

I would be totally adrift without you guys.

Sam

Best,

Marnen Laibow-Koser wrote:

Sam Ward wrote:

The plot thickens....

Thank you guys for you help and how quickly you got back to me. I have
now carefully searched and replaced "Transaction" with "Loan" throughout
my project and, believe it or not, I still have the same error!!

Which tests are failing? Are any tests now passing that were failing,
or vice versa?

The Rails Gods are harsh masters.

Any other ideas? Have I broken something? I am totally flummoxed.

You've probably missed something: your stack trace still seems to
involve Rails' transaction code, which probably means you're still using
the reserved word "transaction" somewhere. (Rails uses that for
database transactions.)

Or, on second thought, maybe not. ActiveRecord wraps all saves in DB
transactions, so you may just be seeing that. I'm not sure in this
case.

Hmm... do you have any code hiding up in the application controller or
applications helpers that still reference transaction? A before filter
that refers to transaction(s)?

Frederick Cheung wrote:

It's calling to_s on something...

Ar Chron wrote:

Hmm... do you have any code hiding up in the application controller?

Between these two snippets, I think we may have come across the issue,
quite early in the development, I decided I didn't like the way the
dates were output as a string. Another site I looked at had a tip
override the to_s in the Date class with a strftime() in the Application
Controller, which _seemed_ to work very well.... now I'm guessing here,
but I think it was possibly calling this as part of the
@transaction.save, converting the form input date into a string and
ActiveRecord not receiving it in the form it wanted. And then it was
sick in my browser.

I did try removing the class override early on but it had no effect. I
think that may have been because I didn't restart the server immediately
after removing it. Now I have done the error seems to have gone away.

I'm assuming I made several silly errors here:

Trusting everything I read on the internet
Not restarting the server after I make changes
Fiddling with things I don't understand
Not doing tests (I still don't know what they are, but I have some
bedtime reading - thanks Marnen!)

I'm really sorry if I've wasted anyones time with my silly error, but I
was seriously considering scrapping the project and starting again so
you really have saved my bacon.

I owe this community a huge debt of gratitude. I will try and keep out
of trouble until I am proficient enough to repay it.

Thank you

Sam

Sam Ward wrote:

I'm assuming I made several silly errors here:

Welcome to the club. We've all been there and done that.

Trusting everything I read on the internet

Trust, but verify comes to mind... and always look at when a certain
post was made. Rails has been changing rapidly...

Not restarting the server after I make changes

That's a good habit to get into...

Fiddling with things I don't understand

Isn't that the basis of learning?

Not doing tests (I still don't know what they are, but I have some
bedtime reading - thanks Marnen!)

Excellent!

I owe this community a huge debt of gratitude. I will try and keep out
of trouble until I am proficient enough to repay it.

No worries.