`stack level too deep` when overwriting `to_json`

I'm using
[acts_as_commentable_with_threading](http://github.com/elight/acts_as_commentable_with_threading/),
and it worked fine until I tried to overwrite the to_json function to
include more details about the associated user.

Here's part of the comment class (which was originally defined in the
plugin, but I moved it to the models folder, a move which seems not to
affect the error):

    def to_json(options = {})
      {
        :id => id,
        :body => body,
        :parent_id => parent_id,
        :created_at => created_at,
        :updated_at => updated_at,
        :user => user ### the line in question
      }.to_json(options)
    end

When the line is included, the server responds accordingly the first
time you request the resource, but every subsequent request (a
particular oddity), I get:

SystemStackError (stack level too deep):
  app/models/comment.rb:21:in `to_json'
  C:1:in `__send__'
  C:1:in `encode'
  C:1:in `__send__'
  C:1:in `encode'
  app/models/item.rb:61:in `to_json'
  app/controllers/items_controller.rb:20
  app/controllers/items_controller.rb:18:in `show'
  C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
  C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'

Removing the line prevents the error from showing up.

I call associations in to_json elsewhere in the application with no
issues.

Steven Xu wrote:

    def to_json(options = {})
      {
        :id => id,
        :body => body,
        :parent_id => parent_id,
        :created_at => created_at,
        :updated_at => updated_at,
        :user => user ### the line in question
      }.to_json(options)
    end

Let's think about what the call to to_json on the Hash is doing...

Each key/value pair will be sent the message to_json. I assume the Hash
takes care of providing the JSON representation of the more basic types
like String, Fixnum, etc. But, what about when to_json gets sent to the
user object.

Given that Item has_one :user then likely User has_many :items. The item
your working with may be contained within an array of user, if user's
to_json includes items. This might explain how you end up with a "stack
level too deep."

Since I don't know how your User model is behaving I can't say for sure
that is the problem, but it is something to consider.

Robert Walker wrote:

Each key/value pair will be sent the message to_json. I assume the Hash
takes care of providing the JSON representation of the more basic types
like String, Fixnum, etc. But, what about when to_json gets sent to the
user object.

Given that Item has_one :user then likely User has_many :items. The item
your working with may be contained within an array of user, if user's
to_json includes items. This might explain how you end up with a "stack
level too deep."

Since I don't know how your User model is behaving I can't say for sure
that is the problem, but it is something to consider.

Thanks for the helpful description. I have made that mistake before (not
with cross-model associations, which are pretty easy to catch, but with
a nested set when I tried to establish aunt and uncle relationships),
and I am sure it's not the case here. Typically, I force jsonning
associations to be manually declared in the options rather than enable
them by default, but I did it here because I ruled it out as a cause.
For the record, the to_json instance method on User doesn't invoke any
associations.

I am at a loss of how to troubleshoot this because:

(1) The "works once then fails thereafter" behaviour is really weird.
(2) I can't reproduce the error through the console (which produces the
expected JSON output, user and all)

Robert Walker wrote:

I am at a loss of how to troubleshoot this because:

(1) The "works once then fails thereafter" behaviour is really weird.

Does it only happen when class caching is turned off ? (don't forget
to restart the server after you change that)

Fred