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