Created
March 28, 2013 17:54
-
-
Save oniram/5265380 to your computer and use it in GitHub Desktop.
decode json deserialize rails model with relationships
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
belongs_to :address | |
accepts_nested_attributes_for :address #Para permitir usar address_attributes | |
def to_custom_json | |
hash = self.to_json(:include => :address, :except => [:address_id, :address_id]) | |
hash.sub("address", "address_attributes") | |
end | |
end | |
#Exemplo de como serializar e deserializar: | |
user = User.new | |
user_json = user.to_custom_json | |
user2 = User.new(ActiveSupport::JSON.decode(user_json)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment