# Validations without AR - going crazy trying to find link

**URL:** <https://www.ruby-forum.com/t/validations-without-ar-going-crazy-trying-to-find-link/54332>\
**Category:** Rails\
**Created:** [March 1, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/validations-without-ar-going-crazy-trying-to-find-link/54332 "2006-03-01T18:26:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tsmith](https://www.ruby-forum.com/u/tsmith)\
**Post date:** [March 1, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/validations-without-ar-going-crazy-trying-to-find-link/54332/1 "2006-03-01T18:26:03Z")

</div>

Hi,

in the past few months someone posted an entry on their blog about  
how to do validations in non-AR classes and I cant find it any more.

Anyone have a link?

Thanks,  
Trevor

–  
Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![tsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tsmith](https://www.ruby-forum.com/u/tsmith)\
**Post date:** [March 1, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/validations-without-ar-going-crazy-trying-to-find-link/54332/2 "2006-03-01T18:39:32Z")

</div>

I can’t find the original blog that I read a while ago but I use it like  
this:

Make a ValidatingBase that the non-AR classes inherit from.

class ValidatingBase  
def save; end  
def update\_attribute; end  
def new\_record?; end  
def self.human\_attribute\_name(arg)  
arg.to\_s  
end  
include ActiveRecord::Validations  
def   
instance\_variable\_get(key)  
end  
include ActiveRecord::Validations  
def initialize(attributes = nil)  
return nil if attributes.nil?

```
attributes.each do |key, value|
  method("#{key}=").call(value)
end

```

end  
end

class Example \< ValidatingBase  
attr\_accessor :request\_postcode, :request\_telephone\_number  
validates\_presence\_of :request\_telephone\_number  
protected  
def validate  
errors.add(:request\_postcode, “must be a valid postcode”) unless  
request\_postcode.blank? || request\_postcode =~  
/\b[A-PR-UWYZ][A-HK-Y0-9][A-HJKSTUW0-9]?[ABEHMNPRVWXY0-9]?  
{1,2}[0-9][ABD-HJLN-UW-Z]{2}/  
end  
end

Hope this helps,

David

On Wed, 2006-03-01 at 09:24 -0800, Trevor S. wrote:

> Hi,
> 
> in the past few months someone posted an entry on their blog about  
> how to do validations in non-AR classes and I cant find it any more.
> 
> Anyone have a link?
> 
> Thanks,  
> Trevor

–  
David Blundell  
100 Percent IT

---

<div class="post-metadata">

**Author:** ![tsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tsmith](https://www.ruby-forum.com/u/tsmith)\
**Post date:** [March 1, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/validations-without-ar-going-crazy-trying-to-find-link/54332/3 "2006-03-01T18:58:16Z")

</div>

Hi David,

well, you have no idea how embarrassing it is but the code you gave  
me is the same code uploaded to the wiki ages ago.

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

And wouldn’t you know it, someone updated that page with a link to  
the (different) technique I was looking for - the one from technoweenie.

Thanks for your help, it got me there in the end.

Regards,  
Trevor

On 1-Mar-06, at 9:39 AM, David Blundell wrote:

> def self.human\_attribute\_name(arg)  
> attributes.each do |key, value|  
> errors.add(:request\_postcode, “must be a valid postcode”)  
> unless request\_postcode.blank? || request\_postcode =~ /\b[A-PR-UWYZ]  
> [A-HK-Y0-9][A-HJKSTUW0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-  
> Z]{2}/  
> end  
> end
> 
> Hope this helps,
> 
> David  
> –  
> Trevor S.  
> [http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![tsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tsmith](https://www.ruby-forum.com/u/tsmith)\
**Post date:** [March 2, 2006, 12:21am UTC](https://www.ruby-forum.com/t/validations-without-ar-going-crazy-trying-to-find-link/54332/4 "2006-03-02T00:21:58Z")

</div>

Hi,

It may have been the following entry;

[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)

and the plugin is available via;

svn: [http://www.realityforge.org/svn/code/active-form/trunk](http://www.realityforge.org/svn/code/active-form/trunk)

On 3/2/06, Trevor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> in the past few months someone posted an entry on their blog about  
> how to do validations in non-AR classes and I cant find it any more.
> 
> Anyone have a link?

–  
Cheers,

Peter D.

---

<div class="post-metadata">

**Author:** ![tsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tsmith](https://www.ruby-forum.com/u/tsmith)\
**Post date:** [March 2, 2006, 3:55am UTC](https://www.ruby-forum.com/t/validations-without-ar-going-crazy-trying-to-find-link/54332/5 "2006-03-02T03:55:29Z")

</div>

Hi Peter,

thanks for the pointer.

By the way (and in case you’re not already aware), when 1.1 comes out  
you can remove the hacks to Dispatcher by doing:

class ActiveForm  
include Reloadable::Subclasses

# rest of class definition here

end

Regards,  
Trevor

–  
Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![tsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tsmith](https://www.ruby-forum.com/u/tsmith)\
**Post date:** [March 2, 2006, 4:54am UTC](https://www.ruby-forum.com/t/validations-without-ar-going-crazy-trying-to-find-link/54332/6 "2006-03-02T04:54:54Z")

</div>

Trevor S. wrote:

> Hi,
> 
> in the past few months someone posted an entry on their blog about  
> how to do validations in non-AR classes and I cant find it any more.
> 
> Anyone have a link?
> 
> Thanks,  
> Trevor
> 
> –  
> Trevor S.  
> [http://somethinglearned.com](http://somethinglearned.com)

I derive table-less models from this AR class (based on the tip posted  
by techno-weenie):

8\<-----------------------------------------------------

# Memory based table-less model to validate input forms that don’t have

an

# underlying table.

# 

# These act like strongly typed structures and inherit ActiveRecords

# validations which make them very useful for form validation.

# 

# Based on tip by technoweenie:

# 

[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)

# 

class TransientRecord \< ActiveRecord::Base

def initialize(attributes=nil)  
super  
# Explicit assignment of the id attribute is necessary because the  
default  
# ActiveRecord (1.0.0) behavior is to omit id from the @attributes  
hash  
# when a new record is instantiated. Until the id entry is added to  
# @attributes with an explicit assignment it won’t be processed by  
the  
# attributes or attributes= methods.  
self.id = nil if self.class.columns\_hash[‘id’]  
self.attributes = attributes if attributes  
end

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

# Because super does not process id attribute (see note in

initialize).  
def attributes=(attributes)  
attributes.each { |k,v| self[k] = v }  
end

end  
8\<-----------------------------------------------------

Derive a concrete table-less model with something like, for example:

class ContactSearch \< TransientRecord

column :id, :integer  
column :first\_name, :string  
column :last\_name, :string  
column :from, :date  
column :to, :date

validates\_numericality\_of :id, :allow\_nil =\> true  
:  
end

Use it in your controller with something like, for example:

@contact\_search = ContactSearch.new(params[:contact\_search])  
if not @contact\_search.valid?  
render :action =\> ‘search’  
else  
:

## Cheers, Stuart

Stuart R.
