mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Merge pull request #1462 from nate00/attribute-overriding--update-docs
Update attribute/association overriding docs
This commit is contained in:
commit
b34d53287e
@ -184,15 +184,11 @@ For more information, see [the Serializer class on GitHub](https://github.com/ra
|
|||||||
|
|
||||||
## Overriding association methods
|
## Overriding association methods
|
||||||
|
|
||||||
If you want to override any association, you can use:
|
To override an association, call `has_many`, `has_one` or `belongs_to` with a block:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class PostSerializer < ActiveModel::Serializer
|
class PostSerializer < ActiveModel::Serializer
|
||||||
attributes :id, :body
|
has_many :comments do
|
||||||
|
|
||||||
has_many :comments
|
|
||||||
|
|
||||||
def comments
|
|
||||||
object.comments.active
|
object.comments.active
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -200,15 +196,11 @@ end
|
|||||||
|
|
||||||
## Overriding attribute methods
|
## Overriding attribute methods
|
||||||
|
|
||||||
If you want to override any attribute, you can use:
|
To override an attribute, call `attribute` with a block:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class PostSerializer < ActiveModel::Serializer
|
class PostSerializer < ActiveModel::Serializer
|
||||||
attributes :id, :body
|
attribute :body do
|
||||||
|
|
||||||
has_many :comments
|
|
||||||
|
|
||||||
def body
|
|
||||||
object.body.downcase
|
object.body.downcase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user