from_xml

from_xml(xml) Instance Public methods Sets the model attributes from an XML string. Returns self. class Person include ActiveModel::Serializers::Xml attr_accessor :name, :age, :awesome def attributes=(hash) hash.each do |key, value| instance_variable_set("@#{key}", value) end end def attributes instance_values end end xml = { name: 'bob', age: 22, awesome:true }.to_xml person = Person.new person.from_xml(xml) # => #<Person:0x007fec5e3b3c40 @a

from_json

from_json(json, include_root=include_root_in_json) Instance Public methods Sets the model attributes from a JSON string. Returns self. class Person include ActiveModel::Serializers::JSON attr_accessor :name, :age, :awesome def attributes=(hash) hash.each do |key, value| send("#{key}=", value) end end def attributes instance_values end end json = { name: 'bob', age: 22, awesome:true }.to_json person = Person.new person.from_json(json) # => #<P

as_json

as_json(options = nil) Instance Public methods Returns a hash representing the model. Some configuration can be passed through options. The option include_root_in_json controls the top-level behavior of as_json. If true, as_json will emit a single root node named after the object's type. The default value for include_root_in_json option is false. user = User.find(1) user.as_json # => { "id" => 1, "name" => "Konata Izumi", "age" => 16, # "created_at" => "2006/08/0

serializable_hash

serializable_hash(options = nil) Instance Public methods Returns a serialized hash of your object. class Person include ActiveModel::Serialization attr_accessor :name, :age def attributes {'name' => nil, 'age' => nil} end def capitalized_name name.capitalize end end person = Person.new person.name = 'bob' person.age = 22 person.serializable_hash # => {"name"=>"bob", "age"=>22} person.serializable_hash(only: :name) # => {"n

password_confirmation=

password_confirmation=(unencrypted_password) Instance Public methods

password=

password=(unencrypted_password) Instance Public methods Encrypts the password into the password_digest attribute, only if the new password is not blank. class User < ActiveRecord::Base has_secure_password validations: false end user = User.new user.password = nil user.password_digest # => nil user.password = 'mUc3m00RsqyRe' user.password_digest # => "$2a$10$4LEA7r4YmNHtvlAvHhsYAeZmk/xeUVtMTYqwIvYY76EW5GUqDiP4."

authenticate

authenticate(unencrypted_password) Instance Public methods Returns self if the password is correct, otherwise false. class User < ActiveRecord::Base has_secure_password validations: false end user = User.new(name: 'david', password: 'mUc3m00RsqyRe') user.save user.authenticate('notright') # => false user.authenticate('mUc3m00RsqyRe') # => user

has_secure_password

has_secure_password(options = {}) Instance Public methods Adds methods to set and authenticate against a BCrypt password. This mechanism requires you to have a password_digest attribute. Validations for presence of password on create, confirmation of password (using a password_confirmation attribute) are automatically added. If you wish to turn off validations, pass validations: false as an argument. You can add more validations by hand if need be. If you don't need the confirmatio

model_name

model_name() Instance Public methods Returns an ActiveModel::Name object for module. It can be used to retrieve all kinds of naming-related information (See ActiveModel::Name for more information). class Person < ActiveModel::Model end Person.model_name # => Person Person.model_name.class # => ActiveModel::Name Person.model_name.singular # => "person" Person.model_name.plural # => "people"

uncountable?

uncountable?(record_or_class) Class Public methods Identifies whether the class name of a record or class is uncountable. ActiveModel::Naming.uncountable?(Sheep) # => true ActiveModel::Naming.uncountable?(Post) # => false