Fernando Perez
2008-Jul-14 13:18 UTC
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, -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Fernando Perez
2008-Jul-14 13:39 UTC
Re: Using ActiveRecord validations on a non ActiveRecord Model
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Fernando Perez
2008-Jul-14 16:26 UTC
Re: Using ActiveRecord validations on a non ActiveRecord Model
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Fernando Perez
2008-Jul-14 19:39 UTC
Re: Using ActiveRecord validations on a non ActiveRecord Model
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
julian
2008-Jul-14 19:45 UTC
Re: Using ActiveRecord validations on a non ActiveRecord Model
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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Fernando Perez
2008-Jul-14 19:59 UTC
Re: Using ActiveRecord validations on a non ActiveRecord Model
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jul-14 20:19 UTC
Re: Using ActiveRecord validations on a non ActiveRecord Model
On Jul 14, 2:18 pm, Fernando Perez <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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. >Stumbled across http://www.neeraj.name/639/validating-non-active-record-models Haven''t tried it myself, but worth a try. Fred --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
julian
2008-Jul-14 20:20 UTC
Re: Using ActiveRecord validations on a non ActiveRecord Model
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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Fernando Perez
2008-Jul-14 20:36 UTC
Re: Using ActiveRecord validations on a non ActiveRecord Model
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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>@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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Brian Hogan
2008-Jul-14 21:16 UTC
Re: Using ActiveRecord validations on a non ActiveRecord Model
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 < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > @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. > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Fernando Perez
2008-Jul-14 21:19 UTC
Re: Using ActiveRecord validations on a non ActiveRecord Model
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---