Rails model without a database

I want to create a Rails model (2.1 and 2.2) with ActiveRecord checks, but without a database table. What is the most widely used approach? I found some plugins that claim to offer this functionality, but many of them do not seem to be widely used or supported. What does the community recommend? Right now I am inclined to come up with my own solution based on this blog post .

+57
ruby ruby-on-rails activerecord
Nov 24 '08 at 23:18
source share
13 answers

I think the blog post you are linking is the best way to go. I would suggest only moving the running methods to the module so as not to pollute your code.

+7
Nov 25 '08 at 15:20
source share

There is a better way in Rails 3: http://railscasts.com/episodes/219-active-model

+65
Sep 30 '10 at 13:29
source share

This is the approach I have used in the past:

In the application /models/tableless.rb

class Tableless < ActiveRecord::Base def self.columns @columns ||= []; end def self.column(name, sql_type = nil, default = nil, null = true) columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null) end # Override the save method to prevent exceptions. def save(validate = true) validate ? valid? : true end end 

In the application /models/foo.rb

 class Foo < Tableless column :bar, :string validates_presence_of :bar end 

In script / console

 Loading development environment (Rails 2.2.2) >> foo = Foo.new => #<Foo bar: nil> >> foo.valid? => false >> foo.errors => #<ActiveRecord::Errors:0x235b270 @errors={"bar"=>["can't be blank"]}, @base=#<Foo bar: nil>> 
+43
Nov 25 '08 at 21:22
source share

Now easier:

 class Model include ActiveModel::Model attr_accessor :var validates :var, presence: true end 

ActiveModel::Model code:

 module ActiveModel module Model def self.included(base) base.class_eval do extend ActiveModel::Naming extend ActiveModel::Translation include ActiveModel::Validations include ActiveModel::Conversion end end def initialize(params={}) params.each do |attr, value| self.public_send("#{attr}=", value) end if params end def persisted? false end end end 

http://api.rubyonrails.org/classes/ActiveModel/Model.html

+13
Dec 18 '15 at 11:45
source share

just create a new file ending in ".rb", following the conventions you are used to (the only value for the file name and class name is underlined for the file name, the camel case for the class name) in your "models /" directory. The key here is not to inherit your model from ActiveRecord (because it is an AR that gives you database functionality). for example: for a new model for cars, create a file called "car.rb" in the model directory and inside your model:

 class Car # here goes all your model stuff end 

edit: btw, if you need attributes in your class, you can use everything here that you use on ruby, just add a couple of lines using "attr_accessor":

 class Car attr_accessor :wheels # this will create for you the reader and writer for this attribute attr_accessor :doors # ya, this will do the same # here goes all your model stuff end 

edit # 2: after reading Mike's comment, I would tell you to go your own way if you want to use all the functions of ActiveRecord, but there is no table in the database. If you just want a regular Ruby class, you might find this solution better;)

+8
Nov 25 '08 at 1:14
source share

There's a screencast about an inactive model model compiled by Ryan Bates. Nice place to start.

Just in case, you have not seen him.

+4
Nov 25 '08 at 15:23
source share

I built a quick mixin to handle this, as suggested by John Topley.

http://github.com/willrjmarshall/Tableless

+3
Jun 18 2018-10-18T00:
source share

How about marking a class as abstract?

 class Car < ActiveRecord::Base self.abstract = true end 

this will tell the rails that the class Car does not have a corresponding table.

[edit]

this will not help you if you need to do something like:

 my_car = Car.new 
+2
Nov 25 '08 at 1:18
source share

Use a gem that you can use. As you say, there are AR-based solutions, but they tend to be fragile.

http://validatable.rubyforge.org/

+2
May 12, '09 at 3:52
source share

For completeness:

Rails now (on V5) has a handy module that you can enable:

include ActiveModel::Model

This allows you to initialize the hash and use checks among other things.

Full documentation here .

+2
Jun 30 '17 at 14:48
source share

Has anyone ever tried to include ActiveRecord::Validations and ActiveRecord::Validations::ClassMethods in an inactive write class and see what happens when you try to configure validators?

I am sure that there are many dependencies between the authentication card and ActiveRecord itself. But you will be able to get rid of these dependencies by decomposing your own authentication structure from the AR authentication structure.

Just an idea.

Update : oopps, this is more or less what was suggested in the message related to your question. Sorry for the violation.

+1
Nov 26 '08 at 10:21
source share

Do as Tiago Pinto said, and you don’t have a model inherited from ActiveRecord :: Base. This will be a regular ruby ​​class that you insert into a file in your app / models / directory. If none of your models have tables, and you do not use the database or ActiveRecord in your application at all, be sure to modify the environment.rb file to have the following line:

 config.frameworks -= [:active_record] 

This should be in the Rails::Initializer.run do |config| block Rails::Initializer.run do |config| .

0
Nov 25 '08 at 16:24
source share

You should check out the PassiveRecord plugin. It provides an ActiveRecord interface for models without a database. It's simple, and less hassle than fighting ActiveRecord.

We use PassiveRecord in combination with the Validatable gem to get the desired OP behavior.

0
Jul 18 '09 at 4:46
source share



All Articles