From 47896b3d08242e641b2bc2c2b8920d4b8e4e7804 Mon Sep 17 00:00:00 2001 From: rusikf Date: Mon, 4 May 2015 11:34:52 +0300 Subject: add docs to include option at ActiveModel::Serialization#serializable_hash [ci skip] --- activemodel/lib/active_model/serialization.rb | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'activemodel/lib/active_model') diff --git a/activemodel/lib/active_model/serialization.rb b/activemodel/lib/active_model/serialization.rb index c06a64c75f..c82c4b67f5 100644 --- a/activemodel/lib/active_model/serialization.rb +++ b/activemodel/lib/active_model/serialization.rb @@ -94,6 +94,37 @@ module ActiveModel # person.serializable_hash(except: :name) # => {"age"=>22} # person.serializable_hash(methods: :capitalized_name) # # => {"name"=>"bob", "age"=>22, "capitalized_name"=>"Bob"} + # + # Example with :include option + # + # class User + # include ActiveModel::Serializers::JSON + # attr_accessor :name, :notes # Emulate has_many :notes + # def attributes + # {'name' => nil} + # end + # end + # + # class Note + # include ActiveModel::Serializers::JSON + # attr_accessor :title, :text + # def attributes + # {'title' => nil, 'text' => nil} + # end + # end + # + # note = Note.new + # note.title = 'Battle of Austerlitz' + # note.text = 'Some text here' + # + # user = User.new + # user.name = 'Napoleon' + # user.notes = [note] + # + # user.serializable_hash + # #=> {"name" => "Napoleon"} + # user.serializable_hash(include: { notes: { only: 'title' }}) + # #=> {"name" => "Napoleon", "notes" => [{"title"=>"Battle of Austerlitz"}]} def serializable_hash(options = nil) options ||= {} -- cgit v1.2.3