jnicklas/carrierwave — pull request #618 by razorjack (2012-02-13 21:18:16+00:00)
Title: Fix for CircularReferenceError

Short story:

```ruby
# @post has an :image uploader

# Each example will raise
# ActiveSupport::JSON::Encoding::CircularReferenceError: object references itself

[@post.image].to_json
{data: @post.image}.to_json
```

Fix: Adding `as_json` method to `CarrierWave::Uploader::Serialization`.

Long story:

Calling `to_json` directly on the uploader works fine - it's implemented in CarrierWave. However, when we add uploader instance to any collection, `to_json` is never called on the uploader. ActiveSupport calls `as_json` method on each object of the collection. Since this method was not implemented, original `as_json` added to `Object` ([see here](https://github.com/rails/rails/blob/master/activesupport/lib/active_support/json/encoding.rb#L145)) is called, exposing all instance variables of the uploader. Since one of them is `@model` containing the very same uploader, we're in a circular reference.

This bug is also causing "stack level too deep" error in Mongoid models.