Rails Model without a table Rails Model without a table ruby ruby

Rails Model without a table


class Model  include ActiveModel::Validations  include ActiveModel::Conversion  extend ActiveModel::Naming  attr_accessor :whatever  validates :whatever, :presence => true  def initialize(attributes = {})    attributes.each do |name, value|      send("#{name}=", value)    end  end  def persisted?    false  endend

attr_accessor will create your attributes and you will create the object with initialize() and set attributes.

The method persisted will tell there is no link with the database. You can find examples like this one:http://railscasts.com/episodes/219-active-model?language=en&view=asciicast

Which will explain you the logic.


The answers are fine for 2013 but since Rails 4 all the database independent features of ActiveRecord are extracted into ActiveModel. Also, there's an awesome official guide for it.

You can include as many of the modules as you want, or as little.

As an example, you just need to include ActiveModel::Model and you can forgo such an initialize method:

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

Just use:

attr_accessor :name, :age


The easiest answer is simply to not subclass from ActiveRecord::Base. Then you can just write your object code.