Forum: Ruby on Rails rails + validations -- place them in application.rb?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
(Guest)
on 2009-04-06 23:33
(Received via mailing list)
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
Frederick C. (Guest)
on 2009-04-07 00:18
(Received via mailing list)
On Apr 6, 6:42 pm, "removed_email_address@domain.invalid" 
<removed_email_address@domain.invalid>
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
Josh R. (Guest)
on 2009-04-07 00:33
(Received via mailing list)
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 C.
<removed_email_address@domain.invalid
Frederick C. (Guest)
on 2009-04-07 01:20
(Received via mailing list)
On Apr 6, 9:32 pm, Josh R. <removed_email_address@domain.invalid> wrote:
>
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
Josh R. (Guest)
on 2009-04-07 04:39
(Received via mailing list)
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 C.
<removed_email_address@domain.invalid
(Guest)
on 2009-04-07 11:33
(Received via mailing list)
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, "removed_email_address@domain.invalid" 
<removed_email_address@domain.invalid>
Frederick C. (Guest)
on 2009-04-07 11:41
(Received via mailing list)
On Apr 7, 3:18 am, "removed_email_address@domain.invalid" 
<removed_email_address@domain.invalid>
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
This topic is locked and can not be replied to.