Wojtek
Wojtek

Reputation: 239

Ruby present model not in DB

I would like to use a model in Rails but not store it in DB. For example let's say I have a RSS reader. I would download some RSS data from other site and then create objects with specific model and show them to use. I don't want to store those objects in databse though. How can I do it?

Upvotes: 0

Views: 94

Answers (3)

jfvanderwalt
jfvanderwalt

Reputation: 343

I think your problem might easily be solved by just creating a class, alternatively you can use ActiveModel, it allows for the same behaviour without storing it in the db.

class RssReader
  #include any behaviour you like
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming
end

There is a very nice railscast on this at:

You can also check this out(Rails 4)

Upvotes: 2

Raghuveer
Raghuveer

Reputation: 2636

In rails 2.3 You can do this by this way:

class OrderSession < ActiveRecord::Base
  def self.columns() @columns ||= []; end


  column :orderable_id, :integer
  column :orderable_type, :string
  column :subscription, :boolean
  column :interval, :integer
  column :quantity, :float
  column :number, :integer
  column :only_with_main_cart, :boolean
  column :start_on, :date

  attr_accessor :choice_item
  attr_accessor :interval_choice_item_1
  attr_accessor :interval_choice_item_2
  validates_presence_of :orderable_id
  validates_presence_of :orderable_type
  validates_presence_of :interval
  validates_numericality_of :quantity, :greater_than => 0
  validates_inclusion_of :subscription, :in => [true, false]
  validates_inclusion_of :only_with_main_cart, :in => [true, false]

end

I am using this for storing cart information before user confirmation

Upvotes: 0

Matt
Matt

Reputation: 14038

You're looking for tableless models, there are plenty of questions on SO about this:

and a handy railscast!

Upvotes: 0

Related Questions