I am building a Rest API using rails-api and active-model-serializer to easily filter the required fields in the JSON. I am also using the has_one
association in these serializers. All I wanted to know is how do I specify a different key name for the has_one
attribute.
That is, I have two models say: Employee
and Address
, and there is a has_one :address
in say EmployeeSerializer
. The response that I get is:
{
id: 1,
address: {
street: "some_street",
city: "some_city"
}
}
But I would like to get the following response:
{
id: 1,
stays: {
street: "some_street",
city: "some_city"
}
}
I tried using has_one :address, :key => :stays
, but that doesn't seem to work.
@janfoeh is right on. I just tested this in version 0.8 and it works just fine
gem "active_model_serializers", "~> 0.8.0"
in your Gemfile$bundle install
has_one :address, key: "stays"
Works great!
As the documentation for the gem says (here), "you probably want to use version 0.8"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With