From da85d944d43527567ab73f12f550b006b4e5bc9b Mon Sep 17 00:00:00 2001 From: Nate Sullivan Date: Sat, 23 Jan 2016 18:50:16 -0800 Subject: [PATCH 1/2] Remove unrelated code from attribute override examples --- docs/general/serializers.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/docs/general/serializers.md b/docs/general/serializers.md index db0c37b4..d6c5ba2a 100644 --- a/docs/general/serializers.md +++ b/docs/general/serializers.md @@ -188,8 +188,6 @@ If you want to override any association, you can use: ```ruby class PostSerializer < ActiveModel::Serializer - attributes :id, :body - has_many :comments def comments @@ -204,9 +202,7 @@ If you want to override any attribute, you can use: ```ruby class PostSerializer < ActiveModel::Serializer - attributes :id, :body - - has_many :comments + attributes :body def body object.body.downcase From 0a937a0fba50ac285803f2bfc81d6cbb7dde135a Mon Sep 17 00:00:00 2001 From: Nate Sullivan Date: Sat, 23 Jan 2016 18:51:48 -0800 Subject: [PATCH 2/2] Use new block-based attribute override in docs --- docs/general/serializers.md | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/docs/general/serializers.md b/docs/general/serializers.md index d6c5ba2a..4014cfe2 100644 --- a/docs/general/serializers.md +++ b/docs/general/serializers.md @@ -184,13 +184,11 @@ For more information, see [the Serializer class on GitHub](https://github.com/ra ## 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 class PostSerializer < ActiveModel::Serializer - has_many :comments - - def comments + has_many :comments do object.comments.active end end @@ -198,13 +196,11 @@ end ## Overriding attribute methods -If you want to override any attribute, you can use: +To override an attribute, call `attribute` with a block: ```ruby class PostSerializer < ActiveModel::Serializer - attributes :body - - def body + attribute :body do object.body.downcase end end