josh.rachner-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2009-Apr-06 17:42 UTC
rails + validations -- place them in application.rb?
I need to put a set of validations in a place that can be accessed by multiple controllers... do I just place them in application.rb? Thanks, Josh --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Apr-06 20:17 UTC
Re: rails + validations -- place them in application.rb?
On Apr 6, 6:42 pm, "josh.rach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <josh.rach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I need to put a set of validations in a place that can be accessed by > multiple controllers...That doesn''t entirely make sense - validations are a model thing. are you trying to share code between models ? Fred> do I just place them in application.rb? > > Thanks, > > Josh--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Josh Rachner
2009-Apr-06 20:32 UTC
Re: rails + validations -- place them in application.rb?
So, the validation is something like this... validate_duplicate_artist(artist_id, venue_id, event_time) rather than a validate_xxx_of type deal, these are more in depth validations... does that make sense? i figured the best place would be somewhere in the model, but with the way it is set up, i figured application.rb would be the best. it needs to be accessed in many places. thanks On Mon, Apr 6, 2009 at 3:17 PM, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Apr 6, 6:42 pm, "josh.rach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <josh.rach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > I need to put a set of validations in a place that can be accessed by > > multiple controllers... > > That doesn''t entirely make sense - validations are a model thing. are > you trying to share code between models ? > > Fred > > > do I just place them in application.rb? > > > > Thanks, > > > > Josh > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Apr-06 21:19 UTC
Re: rails + validations -- place them in application.rb?
On Apr 6, 9:32 pm, Josh Rachner <josh.rach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So, the validation is something like this... > > validate_duplicate_artist(artist_id, venue_id, event_time) > > rather than a validate_xxx_of type deal, these are more in depth > validations... > > does that make sense? i figured the best place would be somewhere in the > model, but with the way it is set up, i figured application.rb would be the > best. it needs to be accessed in many places. >application.rb is a bit of a misnomer (in that the name makes it sounds like it''s some sort of thing for the whole app). It just contains the parent class of all of your controllers, so if what you''re doing isn''t controller related (it isn''t) then it doesn''t belong there. You could stick your validations in a module and include that them into the models that need them or just include them into Active Record itself (google for adding custom validations for some more lengthy explanations). Fred> thanks > > On Mon, Apr 6, 2009 at 3:17 PM, Frederick Cheung <frederick.che...@gmail.com > > > wrote: > > > On Apr 6, 6:42 pm, "josh.rach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <josh.rach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > I need to put a set of validations in a place that can be accessed by > > > multiple controllers... > > > That doesn''t entirely make sense - validations are a model thing. are > > you trying to share code between models ? > > > Fred > > > > do I just place them in application.rb? > > > > Thanks, > > > > Josh--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Josh Rachner
2009-Apr-07 00:38 UTC
Re: rails + validations -- place them in application.rb?
Thanks, that''s just what I was looking for. I''ll just write my own custom validations. Josh On Mon, Apr 6, 2009 at 4:19 PM, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Apr 6, 9:32 pm, Josh Rachner <josh.rach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > So, the validation is something like this... > > > > validate_duplicate_artist(artist_id, venue_id, event_time) > > > > rather than a validate_xxx_of type deal, these are more in depth > > validations... > > > > does that make sense? i figured the best place would be somewhere in the > > model, but with the way it is set up, i figured application.rb would be > the > > best. it needs to be accessed in many places. > > > application.rb is a bit of a misnomer (in that the name makes it > sounds like it''s some sort of thing for the whole app). It just > contains the parent class of all of your controllers, so if what > you''re doing isn''t controller related (it isn''t) then it doesn''t > belong there. You could stick your validations in a module and include > that them into the models that need them or just include them into > Active Record itself (google for adding custom validations for some > more lengthy explanations). > > Fred > > > thanks > > > > On Mon, Apr 6, 2009 at 3:17 PM, Frederick Cheung < > frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > > > > > wrote: > > > > > On Apr 6, 6:42 pm, "josh.rach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <josh.rach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > wrote: > > > > I need to put a set of validations in a place that can be accessed by > > > > multiple controllers... > > > > > That doesn''t entirely make sense - validations are a model thing. are > > > you trying to share code between models ? > > > > > Fred > > > > > > do I just place them in application.rb? > > > > > > Thanks, > > > > > > Josh > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
josh.rachner-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2009-Apr-07 02:18 UTC
Re: rails + validations -- place them in application.rb?
One more thing... I think I made the mistake of making the "set of validations" sound like validate_xxx_of type functions... if I am needing the following functions to be global (this is some old code, but just for example) could i just add them to application_helper? def validate_duplicate_artist(artist) # Query DB for the given artist @result = Artist.find(:first, :conditions => [ "name = ?", artist]) # If no matches, then go ahead and add if @result.nil? # calls parser function to clean up the artist to a general format artist_parsed = validate_artist_parser(artist) # pp artist_parsed Artist.all.each do |artist| # calls parser function to clean up the artist from db to the general format artist_db = validate_artist_parser(artist.name) if artist_parsed == artist_db # if the db artist and crawled artist find a match, then return the id and status of that artist return {:id => artist.id, :status => artist.status} end end return {:id => 0, :status => "dup"} else return {:id => @result.id, :status => @result.status} end end def validate_artist_parser(artist) a = artist.downcase a.gsub!(/ & /, " ") a.gsub!(/ and /, " ") a.gsub!(/ an /, " ") a.gsub!(/ the /, " ") a.gsub!(/ a /, " ") a.gsub!(/\./, "") a.gsub!(/,/, "") a.gsub!(/''/, "") a.gsub!(/-/, "") a.gsub!(/!/, "") a.gsub!(/’/, "") a.gsub!(/ /, "") return a.downcase end These are validations that need to be accessed from an admin tool and a crawler_controller... It just looks like adding to the active record validation.rb file might not be the right place. Maybe I''m wrong? On Apr 6, 12:42 pm, "josh.rach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <josh.rach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I need to put a set of validations in a place that can be accessed by > multiple controllers... do I just place them in application.rb? > > Thanks, > > Josh--~--~---------~--~----~------------~-------~--~----~ 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
2009-Apr-07 07:40 UTC
Re: rails + validations -- place them in application.rb?
On Apr 7, 3:18 am, "josh.rach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <josh.rach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> These are validations that need to be accessed from an admin tool and > a crawler_controller... It just looks like adding to the active record > validation.rb file might not be the right place. Maybe I''m wrong? >Maybe not in this case. Personally the first one at least sounds like something that could be a class method of artist - Artist.find_by_name_trying_really_hard Fred> On Apr 6, 12:42 pm, "josh.rach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <josh.rach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > I need to put a set of validations in a place that can be accessed by > > multiple controllers... do I just place them in application.rb? > > > Thanks, > > > Josh--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---