I'm trying to create a serializer that will give out an object that has several references onto the same object. For example there is an "Lender" object that has two addresses, one of them is the "registration address" and the second is "actual address".
Model
class Lender < ActiveRecord::Base
...
belongs_to :address, foreign_key: 'registration_address_id'
belongs_to :address, foreign_key: 'actual_address_id'
...
end
Serializer
class LenderSerializer < ActiveModel::Serializer
embed :ids, include: true
attributes :id, ...
has_one :address, key: :actual_address_id
has_one :address, key: :registration_address_id
end
When client loads this object from the server there is only one reference included in the JSON output just like this:
{"addresses":[{"id":5,"full_address":"..."}],
"lenders":[{"id":2,"company_title":null,"registration_address_id":5}]}
The problem is that in the output JSON there is no "actual_address_id" and there is no "actual address" in the first array of addresses.
Just in case if someone will need it.
In order to resolve this, I've switched onto JBuilder gem, that gives much more explicit control on the output JSON format.