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

On Apr 6, 6:42 pm, “[email protected][email protected]
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

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.
<[email protected]

On Apr 6, 9:32 pm, Josh R. [email protected] 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

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.
<[email protected]

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, “[email protected][email protected]

On Apr 7, 3:18 am, “[email protected][email protected]
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