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:

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 Perez wrote:

Argh, I finally managed to include the validations, but it seems
ActiveRecord wants to save the model:
--
undefined method `save' for class `Contact'
--

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

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 Perez <rails-mailing-l...@andreas-s.net>

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.

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

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

Fred

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.

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 Perez <rails-mailing-l...@andreas-s.net>

@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.

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_table follows the same idea and should work just fine.

Brian Hogan 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 Perez <

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