Using ActiveRecord validations on a non ActiveRecord Model

Hi,

In order to keep code clean and well organized using MVC, I have created
a model which doesn’t inherit from ActiveRecord as it doesn’t use the
database.

What I would like to do is use ActiveRecord validation such as
validates_presence_of and validates_format_of on the attributes of my
model without saving them. Actually if the object passes validation,
then it should be sent by email.

In my Rails model I tried to load the validations with:

require ‘active_record’

But I keep getting the following error:

undefined method `validates_presence_of’ for Contact:Class

Best regards,

Argh, I finally managed to include the validations, but it seems
ActiveRecord wants to save the model:

undefined method save' for classContact’

I am initializing the object with:

@contact = Contact.new

Isn’t it possible anymore to validate without saving? I found resources
on the Net, but their solutions didn’t work for me.

I have found the following information :
http://www.neeraj.name/639/validating-non-active-record-models

But I don’t like the way it was coded, I find it too complicated.

Fernando P. wrote:

Argh, I finally managed to include the validations, but it seems
ActiveRecord wants to save the model:

undefined method save' for classContact’

I am initializing the object with:

@contact = Contact.new

Isn’t it possible anymore to validate without saving? I found resources
on the Net, but their solutions didn’t work for me.

I don’t understand why when I define a “save” class method, I still get
this error message. This is a Ruby problem, and it bugs me out that I
can’t get my head around it.

I have:

class Contact
def self.save
end
end

And it still spits me out the same error message, as if I hadn’t written
anything… Anyway I don’t understand why the validation wants to save
the object, I never told it to.

I think it’s calling Contact#save not Contact.save.

try:

class Contact
def save; end
end

On Jul 14, 3:39 pm, Fernando P. [email protected]

julian wrote:

I think it’s calling Contact#save not Contact.save.

try:

class Contact
def save; end
end

On Jul 14, 3:39�pm, Fernando P. [email protected]

Hi julian,

I forgot to mention, but I tried both at the same time, and I still get
the error. I think I’ll simply forget about AR’s validations and have to
rewrite my own ones.

On Jul 14, 2:18 pm, Fernando P. [email protected]
wrote:

Stumbled across
http://www.neeraj.name/639/validating-non-active-record-models

Haven’t tried it myself, but worth a try.

Fred

julian wrote:

I’m not entirely sure but validations in ActiveRecord appear to work
by using alias_method_chain on the base save method. If you put up
your whole model we might get it working. First of all though, make
sure the Contact#save method definition is before the include call so
that alias_method_chain has something to alias.

On Jul 14, 3:59Â pm, Fernando P. [email protected]

@Fred: I already talked about that webpage, and said I didn’t like it.
It looks like a lot of lines of code for getting a little feature to
work.

@Julian: you are certainly right, my include was before the Contact#save
definition.

In the mean time I have found this nifty little plugin:
http://agilewebdevelopment.com/plugins/activerecord_base_without_table

It works like a charm with Rails 2.1, and gives on top of validation,
the ability to have a nicely crafted site wide error message.

I’m not entirely sure but validations in ActiveRecord appear to work
by using alias_method_chain on the base save method. If you put up
your whole model we might get it working. First of all though, make
sure the Contact#save method definition is before the include call so
that alias_method_chain has something to alias.

On Jul 14, 3:59Â pm, Fernando P. [email protected]

A little late to the party but I wrote this over 2 years ago:

http://www.bphogan.com/learn/pdf/tableless_contact_form.pdf

The plugin at
http://agilewebdevelopment.com/plugins/activerecord_base_without_tablefollows
the same idea and should work just fine.

On Mon, Jul 14, 2008 at 3:36 PM, Fernando P. <

Brian H. wrote:

A little late to the party but I wrote this over 2 years ago:

http://www.bphogan.com/learn/pdf/tableless_contact_form.pdf

The plugin at
http://agilewebdevelopment.com/plugins/activerecord_base_without_tablefollows
the same idea and should work just fine.

On Mon, Jul 14, 2008 at 3:36 PM, Fernando P. <

Thank you Brian, your tutorial is very detailed and is a good wrap up of
the subject.