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

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

to_xml

to_xml(options = {}, &block) Instance Public methods Returns XML representing the model. Configuration can be passed through options. Without any options, the returned XML string will include all the model's attributes. user = User.find(1) user.to_xml <?xml version="1.0" encoding="UTF-8"?> <user> <id type="integer">1</id> <name>David</name> <age type="integer">16</age> <created-at type="dateTime">2011-01-30T22:29:23

human_attribute_name

human_attribute_name(attribute, options = {}) Instance Public methods Transforms attribute names into a more human format, such as âFirst nameâ instead of âfirst_nameâ. Person.human_attribute_name("first_name") # => "First name" Specify options with additional translating options.

i18n_scope

i18n_scope() Instance Public methods Returns the i18n_scope for the class. Overwrite if you want custom lookup.

lookup_ancestors

lookup_ancestors() Instance Public methods When localizing a string, it goes through the lookup returned by this method, which is used in ActiveModel::Name#human, ActiveModel::Errors#full_messages and #human_attribute_name.

after_validation

after_validation(*args, &block) Instance Public methods Defines a callback that will get called right after validation happens. class Person include ActiveModel::Validations include ActiveModel::Validations::Callbacks attr_accessor :name, :status validates_presence_of :name after_validation :set_status private def set_status self.status = errors.empty? end end person = Person.new person.name = '' person.valid? # => false person.status # => false

before_validation

before_validation(*args, &block) Instance Public methods Defines a callback that will get called right before validation happens. class Person include ActiveModel::Validations include ActiveModel::Validations::Callbacks attr_accessor :name validates_length_of :name, maximum: 6 before_validation :remove_whitespaces private def remove_whitespaces name.strip! end end person = Person.new person.name = ' bob ' person.valid? # => true person.name # =&

attribute_method?

attribute_method?(attribute) Instance Public methods Returns true if attribute is an attribute method, false otherwise. class Person include ActiveModel::Validations attr_accessor :name end User.attribute_method?(:name) # => true User.attribute_method?(:age) # => false

clear_validators!

clear_validators!() Instance Public methods Clears all of the validators and validations. Note that this will clear anything that is being used to validate the model for both the validates_with and validate methods. It clears the validators that are created with an invocation of validates_with and the callbacks that are set by an invocation of validate. class Person include ActiveModel::Validations validates_with MyValidator validates_with OtherValidator, on: :create valid