# Validations for non-ActiveRecord Model Objects

**URL:** <https://www.ruby-forum.com/t/validations-for-non-activerecord-model-objects/49325>\
**Category:** Rails\
**Created:** [December 2, 2005, 4:19pm UTC](https://www.ruby-forum.com/t/validations-for-non-activerecord-model-objects/49325 "2005-12-02T16:19:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter.j.donald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@peter.j.donald](https://www.ruby-forum.com/u/peter.j.donald)\
**Post date:** [December 2, 2005, 4:19pm UTC](https://www.ruby-forum.com/t/validations-for-non-activerecord-model-objects/49325/1 "2005-12-02T16:19:54Z")

</div>

Hi,

I just worked on getting validations for non-ActiveRecord model  
objects working. I essentially implemented an ActiveForm object that  
you can subclass for your own non-database form objects. The file is  
available from my blog [1] and I wouldn’t mind some feedback on  
whether my assumptions are right. It seems to work for me and makes it  
much easier to write non-database bound forms.

[1]  
[http://www.realityforge.org/articles/2005/12/02/validations-for-non-activerecord-model-objects](http://www.realityforge.org/articles/2005/12/02/validations-for-non-activerecord-model-objects)

–  
Cheers,

Peter D.

---

<div class="post-metadata">

**Author:** ![peter.j.donald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@peter.j.donald](https://www.ruby-forum.com/u/peter.j.donald)\
**Post date:** [December 2, 2005, 5:41pm UTC](https://www.ruby-forum.com/t/validations-for-non-activerecord-model-objects/49325/2 "2005-12-02T17:41:12Z")

</div>

On 12/2/05, Peter D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Cheers,
> 
> Peter D.

Here’s my approach to the problem. Instead of making my own class and  
reimplementing the validations, I just removed the code that grabs the  
table info:

class Contact \< 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

column :name, :string  
column :city, :string  
column :state, :string  
column :phone, :string  
column :email\_address, :string

validates\_presence\_of :name, :email\_address  
end

It’s still technically a model, so you can call #save on it. But just  
calling valid? works fine.

[http://rails.techno-weenie.net/tip/2005/11/19/validate\_your\_forms\_with\_a\_table\_less\_model](http://rails.techno-weenie.net/tip/2005/11/19/validate_your_forms_with_a_table_less_model)

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![peter.j.donald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@peter.j.donald](https://www.ruby-forum.com/u/peter.j.donald)\
**Post date:** [December 6, 2005, 4:13am UTC](https://www.ruby-forum.com/t/validations-for-non-activerecord-model-objects/49325/3 "2005-12-06T04:13:37Z")

</div>

Hi,

It turns out that there is a wiki page[1] that already describes this  
technique. Who knew? 🙂 It is a little dated but I wish I had wiki’ed  
before attempting this 😉

[1]  
[http://wiki.rubyonrails.com/rails/pages/HowToUseValidationsWithoutExtendingActiveRecord](http://wiki.rubyonrails.com/rails/pages/HowToUseValidationsWithoutExtendingActiveRecord)

On 12/3/05, Peter D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Cheers,
> 
> Peter D.

–  
Cheers,

Peter D.

---

<div class="post-metadata">

**Author:** ![peter.j.donald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@peter.j.donald](https://www.ruby-forum.com/u/peter.j.donald)\
**Post date:** [December 6, 2005, 5:30am UTC](https://www.ruby-forum.com/t/validations-for-non-activerecord-model-objects/49325/4 "2005-12-06T05:30:31Z")

</div>

On 12/5/05, Peter D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> It turns out that there is a wiki page[1] that already describes this  
> technique. Who knew? 🙂 It is a little dated but I wish I had wiki’ed  
> before attempting this 😉
> 
> [1] [Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowToUseValidationsWithoutExtendingActiveRecord)

I knew about that, but my version doesn’t mess with rails as much. I  
prefer my 3 lines to set it up vs changes in the dark corners of the  
dispatcher libs. YMMV

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)
