# Form validation and processing w/o model (no Db/ActiveRecord)

**URL:** <https://www.ruby-forum.com/t/form-validation-and-processing-w-o-model-no-db-activerecord/193370>\
**Category:** Rails\
**Created:** [August 16, 2010, 7:24pm UTC](https://www.ruby-forum.com/t/form-validation-and-processing-w-o-model-no-db-activerecord/193370 "2010-08-16T19:24:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [August 16, 2010, 7:24pm UTC](https://www.ruby-forum.com/t/form-validation-and-processing-w-o-model-no-db-activerecord/193370/1 "2010-08-16T19:24:35Z")

</div>

All -  
Trying not to fight rails on this, basically have a form and all the  
same as  
would be with an ActiveRecord model except that there is no table. I  
started  
out using this article:

> <https://stackoverflow.com/questions/315850/rails-model-without-database>

…which is kind of a hack by inheriting ActiveRecord in to non-AR model  
but  
am running into some problems and just have the feeling that I am  
fighting  
rails and going to spend more time on this than the benefit warrants.

All am looking to do is receive and validate form input, and ideally  
have  
the luxuries like Class.new(params[:object]),  
Class.update\_attributes(params[:object]), and for the form to do similar  
highlighting and error feedback messages as AR.

I found this plug-in: active\_form  
([GitHub - nesquena/active\_form: Submission to github of validation plugin for non-activerecord models.](http://github.com/nesquena/active_form)).  
Wondering if anyone has used or has better ideas.

David

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [August 16, 2010, 7:27pm UTC](https://www.ruby-forum.com/t/form-validation-and-processing-w-o-model-no-db-activerecord/193370/2 "2010-08-16T19:27:36Z")

</div>

Here is what I do:

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  
end

Class Contact \< Tableless  
column :address, :string  
column :message, :text

validates\_presence\_of :message, :address  
end

It works perfectly in Rails 2.3.8, it might break in Rails 3 I haven’t  
tested yet.

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [August 16, 2010, 7:35pm UTC](https://www.ruby-forum.com/t/form-validation-and-processing-w-o-model-no-db-activerecord/193370/3 "2010-08-16T19:35:43Z")

</div>

On Mon, Aug 16, 2010 at 1:27 PM, Fernando P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:  
Here is what I do:

class Tableless \< ActiveRecord::Base  
def self.columns  
@columns ||= [];  
end

def self.column(name, sql\_type = nil, default = nil, null = true)  
columns \<\< ActiveRecord::

> validates\_presence\_of :message, :address  
> end
> 
> It works perfectly in Rails 2.3.8, it might break in Rails 3 I haven’t  
> tested yet.

Thanks Fernando. That is exactly what I was doing (but getting an error)  
but  
your telling me it works perfectly for you made me look further and I  
found  
an error in a field name that I was validating — so now all works  
well.  
Good to have confirmation that this structure is being used successfully  
by  
others.

David
