← Back to blog
· 1 min read · ActiveModel Rails

ActiveModel in Rails3

In Rails3 Active Model feature is added that helps to use feature of active record class for a non active record class.In Active Model you can make Any Ruby Object Feel Like Active Record and can get Rails-style models with validations, serialization, callbacks, dirty tracking, internationalization, attributes, observers and all the other Rails goodness.

Here is the example -: {% codeblock lang:ruby%} class Message include ActiveModel::Validations include ActiveModel::Conversion include ActiveModel::Naming

attraccessor :name,:email,:textmessage

validatespresenceof :name validatespresenceof :email validatespresenceof :textmessage validateslengthof :textmessage, :maximum => 500

def initialize(attributes={}) attributes.each do |name, value| send("#{name}=", value) end end

end

{% endcodeblock %} ```ruby @message = Message.new({:name => "rays", :email => 'abc@example.com', :text_message => "this my text message"})

@message.valid? => true

@message = Message.new({:name => "rays", :text_message => "this my text message"})

@message.valid? => false

@message.errors will give error "Email can't be blank" ```