josevalim/active_model_serializers — Issue #12: Can't use Polymorphic Attributes
Author: Adman65
Created: 2011-12-12 10:50:08+00:00
State: open

I am unsure of how to do this at this point, but here is my proposed solution.

```ruby
has_one :reference, :polymorphic => true
```

Then at serialization time, instantiate a serialize based on the class name of the polymorphic object and call serialize with that object and scope.

I also thought you could have a `ReferenceSerializer` which would determine how to serialize whatever object is passed to it. However, I just realized that would break down if you had multiple polymorphic associations with the same name. IE a Todo can have a reference and an attachment can have a reference. Then you'd have to pass `:serializer => TodoReferenceSerializer` etc. Thus, I think the first choice is better.

I am not sure exactly how that code would work. I think a subclass of the `HasOne` association would do the trick. This method is also easily extendable to `HasMany` as well.

If you think this solution is correct, then I'll write the code for this as I need it for my app (again :D)