aasm: may_*? method `wrong number of arguments (given 1, expected 0)`

  • AASM 4.12.0
  • Rails 5.0.1

may_contract? method will be got below error.

ActionView::Template::Error (wrong number of arguments (given 1, expected 0)):

The error occur with below code.

    event :contract do
      transitions from: [:authorized], to: :contracted, guard: :can_contract?
    end

  ・・・・

  def can_contract?
    self.authorizer_id.present?
  end

But when rewite with below code. The error disappears.

    event :contract do
      transitions from: [:authorized], to: :contracted do
        guard do
          self.can_contract?
        end
      end
    end

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 4
  • Comments: 27 (17 by maintainers)

Commits related to this issue

Most upvoted comments

Hi, I have this issue on 4.12.1, but it wasn’t fixed upgrading to any of the later versions I tried them one by one. Can anybody confirm if this issue is still present, if I need to upgrade something else or if there is a workaround? rails 4.2.5 / rspec-rails 3.0 (still happening on 3.8), and aasm 4.11.1 (tried every version to 5.0 and it’s still happening)

Hello @anilmaurya ,

I confirmed it solve the issue (I fixed syntax error)

result = (record.__send__(:method, code.to_sym).arity > 0 ? record.__send__(code, *args) : record.__send__(code))

@teohm This bug fix is released in 4.12.1

Hi @hirox ,

Thank you for your help so far. Could you help little more ?

Can you confirm rspec-mocks destroys arity only for 0 argument and maintains arity if arguments is present ?

If so then

result = (record.__send__(:method, code.to_sym).arity > 0 ? record.__send__(code, *args)) : record.__send__(code)

will solve this issue.

Hi @klaustopher , Thank you for keeping eye on this issue.

I am running of time for last few weeks. Any help is much appreciated.

A failing test will help in solving issues quicker.

It’s very strange, that this issue has been open for 22 days and nobody who’s involved in the project has even acknowledged that a bug was introduced that breaks behavior that is documented in the readme.

Hello. I have no idea to fix it, but I found the cause of the error.

With this commit, https://github.com/aasm/aasm/commit/c3b6454e8e9ca499e57aac4c74ec18bf9fa028b4#diff-6323dc24ff3a8a3e52b3b26d63e926cdR126 (#404, #406), args.unshift(nil) if args.blank? was added and args will be [nil] although there is no arguments. This change causes validation error because can_contract? expects 0 arguments.