diff options
author | Robin Dupret <robin.dupret@gmail.com> | 2015-06-07 15:53:53 +0200 |
---|---|---|
committer | Robin Dupret <robin.dupret@gmail.com> | 2015-06-07 15:53:53 +0200 |
commit | 911f189f88b49df7fc08e6a82b8f89eed86ab2b5 (patch) | |
tree | 4e4530555e8f2672b16bbefe8d3e55b54f59a263 | |
parent | 27eccc27cbe987be04bb97b49aff1d7fd118634c (diff) | |
parent | 47896b3d08242e641b2bc2c2b8920d4b8e4e7804 (diff) | |
download | rails-911f189f88b49df7fc08e6a82b8f89eed86ab2b5.tar.gz rails-911f189f88b49df7fc08e6a82b8f89eed86ab2b5.tar.bz2 rails-911f189f88b49df7fc08e6a82b8f89eed86ab2b5.zip |
Merge pull request #20004 from rusikf/patch-1
add docs to include option at ActiveModel::Serialization [ci skip]
-rw-r--r-- | activemodel/lib/active_model/serialization.rb | 31 |
1 files changed, 31 insertions, 0 deletions
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 <tt>:include</tt> 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 ||= {} |