Getting Stack level too deep after add "monetize" method for my models. #38

Closed
guilherme opened this Issue Sep 24, 2012 · 9 comments

Comments

Projects
None yet
3 participants
@guilherme

Hello,

I don't know how to identify it but the thing is: after add the 'monetize' method for my model attributes i'm getting:

running rake spec:

/Users/guilherme/.rvm/gems/ruby-1.9.3-p194@gemset/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780: stack level too deep (SystemStackError)
rake aborted!

running spork:

Using RSpec
Preloading Rails environment
Loading Spork.prefork block...
stack level too deep (SystemStackError)
/Users/guilherme/.rvm/gems/ruby-1.9.3-p194@gemset/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:240

I'm using:

  • rails (3.2.3)
    • activemodel (3.2.3)
    • activerecord (3.2.3)
    • activeresource (3.2.3)
    • activesupport (3.2.3)

There's something wrong with compatibility with 3.2.3 ?

@guilherme

This comment has been minimized.

Show comment Hide comment
@guilherme

guilherme Sep 24, 2012

I think there's nothing with the gem load, because when i remove the 'monetize' methods from my models the specs load fine.

I've finally find some nice clue about where it's happening (i've removed from all my classes the 'monetize' method, and inserted on the more simpler class):

  1. SimplerClass
    Failure/Error: Unable to find matching line from backtrace
    SystemStackError:
    stack level too deep

    /Users/guilherme/.rvm/gems/ruby-1.9.3-p194@gemset/gems/money-rails-0.6.0/lib/money-rails/active_record/monetizable.rb:53

I think there's nothing with the gem load, because when i remove the 'monetize' methods from my models the specs load fine.

I've finally find some nice clue about where it's happening (i've removed from all my classes the 'monetize' method, and inserted on the more simpler class):

  1. SimplerClass
    Failure/Error: Unable to find matching line from backtrace
    SystemStackError:
    stack level too deep

    /Users/guilherme/.rvm/gems/ruby-1.9.3-p194@gemset/gems/money-rails-0.6.0/lib/money-rails/active_record/monetizable.rb:53

@alup

This comment has been minimized.

Show comment Hide comment
@alup

alup Sep 24, 2012

Owner

Can you paste the failing spec and model code in order to be able to help you?

Owner

alup commented Sep 24, 2012

Can you paste the failing spec and model code in order to be able to help you?

@alup

This comment has been minimized.

Show comment Hide comment
@alup

alup Oct 1, 2012

Owner

@guilherme Any progress on this?

Owner

alup commented Oct 1, 2012

@guilherme Any progress on this?

@guilherme

This comment has been minimized.

Show comment Hide comment
@guilherme

guilherme Oct 1, 2012

The code is very simple: i've just putted monetize. and then it gave me this stack level too deep. No more information than this is relevant ;(

I've ended with this monkey patch and unfortunately not using money-rails :(
https://gist.github.com/3815120

The code is very simple: i've just putted monetize. and then it gave me this stack level too deep. No more information than this is relevant ;(

I've ended with this monkey patch and unfortunately not using money-rails :(
https://gist.github.com/3815120

@guilherme

This comment has been minimized.

Show comment Hide comment
@guilherme

guilherme Oct 1, 2012

rspec does not give me more information than this failing spec :(

rspec does not give me more information than this failing spec :(

@anthonator

This comment has been minimized.

Show comment Hide comment
@anthonator

anthonator Oct 4, 2012

Contributor

I've been running into this issue as well in Rails 3.2.8. It looks like you have some sort of recursive semantic error in your monetizeable class.

define_method name do |*args|
  amount = send(subunit_name, *args) #LOOK HERE: If name == subunit_name an infinite loop occurs
  attr_currency = send("currency_for_#{name}")

  # Dont create a new Money instance if the values haven't changed
  memoized = instance_variable_get("@#{name}")
  return memoized if memoized && memoized.cents == amount &&
  memoized.currency == attr_currency

  amount = Money.new(amount, attr_currency) unless amount.blank?

  instance_variable_set "@#{name}", amount
end

If name == subunit_name we run into an infinite loop. That's what my testing has concluded.

Contributor

anthonator commented Oct 4, 2012

I've been running into this issue as well in Rails 3.2.8. It looks like you have some sort of recursive semantic error in your monetizeable class.

define_method name do |*args|
  amount = send(subunit_name, *args) #LOOK HERE: If name == subunit_name an infinite loop occurs
  attr_currency = send("currency_for_#{name}")

  # Dont create a new Money instance if the values haven't changed
  memoized = instance_variable_get("@#{name}")
  return memoized if memoized && memoized.cents == amount &&
  memoized.currency == attr_currency

  amount = Money.new(amount, attr_currency) unless amount.blank?

  instance_variable_set "@#{name}", amount
end

If name == subunit_name we run into an infinite loop. That's what my testing has concluded.

@anthonator

This comment has been minimized.

Show comment Hide comment
@anthonator

anthonator Oct 4, 2012

Contributor
if name
  name = name.to_s
elsif subunit_name =~ /_cents$/
  name = subunit_name.sub(/_cents$/, "")
else
  # FIXME: provide a better default
  name = subunit_name << "_money"
end

Using << causes concatenation on subunit_name making name == subunit_name which is causing the infinite loop in the scenario I documented above. If you use a join rather than a concat on subunit_name it should fix the problem.

Unless I'm missing something?

Contributor

anthonator commented Oct 4, 2012

if name
  name = name.to_s
elsif subunit_name =~ /_cents$/
  name = subunit_name.sub(/_cents$/, "")
else
  # FIXME: provide a better default
  name = subunit_name << "_money"
end

Using << causes concatenation on subunit_name making name == subunit_name which is causing the infinite loop in the scenario I documented above. If you use a join rather than a concat on subunit_name it should fix the problem.

Unless I'm missing something?

@alup

This comment has been minimized.

Show comment Hide comment
@alup

alup Oct 4, 2012

Owner

@guilherme Can you confirm that the issue has been fixed? Just use the github repository instead of the gem provided by rubygems.org (gem "money-rails", :git => "git://github.com/RubyMoney/money-rails.git")

Owner

alup commented Oct 4, 2012

@guilherme Can you confirm that the issue has been fixed? Just use the github repository instead of the gem provided by rubygems.org (gem "money-rails", :git => "git://github.com/RubyMoney/money-rails.git")

@alup

This comment has been minimized.

Show comment Hide comment
@alup

alup Oct 7, 2012

Owner

Closing this, as this is supposed to be fixed from #43

Owner

alup commented Oct 7, 2012

Closing this, as this is supposed to be fixed from #43

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment