Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack overflow when using classy_enum in the render function #7

Closed
rantav opened this issue Nov 10, 2011 · 11 comments
Closed

Stack overflow when using classy_enum in the render function #7

rantav opened this issue Nov 10, 2011 · 11 comments

Comments

@rantav
Copy link

rantav commented Nov 10, 2011

Hi, I'm seeing some weird behavior that I can't explain and it seems to be related to classy_enum.
In my model I have:

class Message < ActiveRecord::Base
  classy_enum_attr :message_type

And I have the required enums and everything and it seems to work great. (I've used classy_enum in the past, so it's not my first enum. Other times didn't render a model with enums though)

The problem is that when I want to render a @message in the controller I get a stack overflow and sometimes (but not always) a complete server crash with class_enum in the stack

Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/classy_enum-1.3.0/lib/classy_enum/class_methods.rb:58:in `const_get': uninitialized constant MessageTypeUser (NameError)
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/classy_enum-1.3.0/lib/classy_enum/class_methods.rb:58:in `build'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/classy_enum-1.3.0/lib/classy_enum/attributes.rb:39:in `block (2 levels) in classy_enum_attr'
    from /Users/ran/dev/invi-backend/app/models/message.rb:16:in `to_s'
    from /Users/ran/dev/invi-backend/lib/messaging.rb:22:in `block in send'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/eventmachine-0.12.10/lib/em/deferrable.rb:141:in `call'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/eventmachine-0.12.10/lib/em/deferrable.rb:141:in `set_deferred_status'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/eventmachine-0.12.10/lib/em/deferrable.rb:180:in `fail'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/pusher-0.8.3/lib/pusher/channel.rb:51:in `block in trigger_async'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/eventmachine-0.12.10/lib/em/deferrable.rb:141:in `call'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/eventmachine-0.12.10/lib/em/deferrable.rb:141:in `set_deferred_status'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/eventmachine-0.12.10/lib/em/deferrable.rb:180:in `fail'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/em-http-request-0.3.0/lib/em-http/client.rb:310:in `unbind'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/eventmachine-0.12.10/lib/eventmachine.rb:1417:in `event_callback'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/thin-1.2.11/lib/thin/backends/base.rb:61:in `start'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/thin-1.2.11/lib/thin/server.rb:159:in `start'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/rack-1.2.3/lib/rack/handler/thin.rb:14:in `run'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/rack-1.2.3/lib/rack/server.rb:217:in `start'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands/server.rb:65:in `start'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands.rb:30:in `block in <top (required)>'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands.rb:27:in `tap'
    from /Users/ran/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands.rb:27:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

It's hard to say what exactly is causing this.... but when I remove the classy enum definition in the model the crash doesn't happen.

Does this ring a bell? Have you any idea why is this or how to solve that?

thanks!

@beerlington
Copy link
Owner

I have not seen that issue before. Could you post your MessageType enum class so I can try and reproduce it locally?

@rantav
Copy link
Author

rantav commented Nov 10, 2011

Hi Pete, thanks for the quick reply. I should say that I have no evidence that this errors stems explicitly from classy_enum, it could well be related to other things in my setup (although I wouldn't know what else to guess...) the only kind of evidence I see is that when I comment out the classy_enum_attr :message_type in the model then MessagesController.render is successful (and includes the message_type field from the db in the resulting json) and when it's in place then I see the stack overflow (and sometimes even crash).

Here's the enum code. pretty simple....

class MessageType < ClassyEnum::Base
  enum_classes :user, :system
end

class MessageTypeUser < MessageType
end

class MessageTypeSystem < MessageType
end

@beerlington
Copy link
Owner

I can't think of anything off hand that would be causing since you are definitely using it the way it was intended. I'll dig into it more this evening and see if I can reproduce.

@rantav
Copy link
Author

rantav commented Nov 10, 2011

Thanks, I appreciate it, I hope you can find something. I'm surprised by this as well and I can't imagine what could be causing this weird behavior. The only think not weird here is that it happens all the time consistently :(

@beerlington
Copy link
Owner

Have you tried upgrading to 1.3.1? I had fixed an issue in that version related to converting objects to JSON, and I'm wondering if that will fix your problem as well.

@rantav
Copy link
Author

rantav commented Nov 11, 2011

1.3.1 fixes that for me thanks!
looks like it was the json recursion thing. What's interesting is that from the console I was able to call as_json and that was ok, but probably render would do something else and recurse to death...

@rantav rantav closed this as completed Nov 11, 2011
@rantav
Copy link
Author

rantav commented Nov 11, 2011

Actually, there is still a small issue - or maybe just a question.
The json looks has "message_type":{"to_s":"user","index":1}
While I would expect is to have: "message_type":"user"
Is this by design

@beerlington
Copy link
Owner

I think it's the way that Rails automatically converts the object to JSON and was not something I considered. It probably makes sense for me to add an option that lets you choose whether you want the attribute represented as the value's string or as a JSON object.

Not sure how I'd implement it yet, but I think something like this would make sense:

classy_enum_attr :message_type, :serialize_as_json => false

@beerlington beerlington reopened this Nov 11, 2011
@rantav
Copy link
Author

rantav commented Nov 11, 2011

if you implement that it'd be cool. would certainly make the json response
cleaner.

On Fri, Nov 11, 2011 at 6:25 PM, Pete B <
reply@reply.github.com>wrote:

I think it's the way that Rails automatically converts the object to JSON
and was not something I considered. It probably makes sense for me to add
an option that lets you choose whether you want the attribute represented
as the value's string or as a JSON object.

Not sure how I'd implement it yet, but I think something like this would
make sense:

classy_enum_attr :message_type, :serialize_as_json => false

Reply to this email directly or view it on GitHub:
#7 (comment)

/Ran
http://tavory.com

@beerlington
Copy link
Owner

Just pushed an updated gem v 1.3.2 which adds the serialize_as_json option. I decided that the default should not serialize the enum object and simply returns a string. This means you will not need to make any changes to your code to get the behavior you were expecting.

@rantav
Copy link
Author

rantav commented Nov 12, 2011

Awesome man, thanks!

On Nov 12, 2011, at 10:51 PM, Pete B
reply@reply.github.com
wrote:

Just pushed an updated gem v 1.3.2 which adds the serialize_as_json option. I decided that the default should not serialize the enum object and simply returns a string. This means you will not need to make any changes to your code to get the behavior you were expecting.


Reply to this email directly or view it on GitHub:
#7 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants