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:
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.
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)