mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Merge pull request #99 from matthewrobertson/master
Documentation on how to add custom attributes
This commit is contained in:
commit
e122675373
@ -107,6 +107,37 @@ class PostSerializer < ActiveModel::Serializer
|
|||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Custom Attributes
|
||||||
|
|
||||||
|
If you would like customize your JSON to include things beyond the simple
|
||||||
|
attributes of the model, you can override its `serializable_hash` method
|
||||||
|
to return anything you need. For example:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class Person < ActiveRecord::Base
|
||||||
|
|
||||||
|
def full_name
|
||||||
|
"#{first_name} #{last_name}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def serializable_hash options = nil
|
||||||
|
hash = super(options)
|
||||||
|
hash["full_name"] = full_name
|
||||||
|
hash
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
The attributes returned by `serializable_hash` are then available in your serializer
|
||||||
|
as usual:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class PersonSerializer < ActiveModel::Serializer
|
||||||
|
attributes :first_name, :last_name, :full_name
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
## Associations
|
## Associations
|
||||||
|
|
||||||
For specified associations, the serializer will look up the association and
|
For specified associations, the serializer will look up the association and
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user