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