# Rails + validations -- place them in application.rb?

**URL:** <https://www.ruby-forum.com/t/rails-validations-place-them-in-application-rb/164869>\
**Category:** Rails\
**Created:** [April 6, 2009, 9:33pm UTC](https://www.ruby-forum.com/t/rails-validations-place-them-in-application-rb/164869 "2009-04-06T21:33:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 6, 2009, 9:33pm UTC](https://www.ruby-forum.com/t/rails-validations-place-them-in-application-rb/164869/1 "2009-04-06T21:33:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 6, 2009, 10:18pm UTC](https://www.ruby-forum.com/t/rails-validations-place-them-in-application-rb/164869/2 "2009-04-06T22:18:29Z")

</div>

On Apr 6, 6:42 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 6, 2009, 10:33pm UTC](https://www.ruby-forum.com/t/rails-validations-place-them-in-application-rb/164869/3 "2009-04-06T22:33:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 6, 2009, 11:20pm UTC](https://www.ruby-forum.com/t/rails-validations-place-them-in-application-rb/164869/4 "2009-04-06T23:20:36Z")

</div>

On Apr 6, 9:32 pm, Josh R. [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 7, 2009, 2:39am UTC](https://www.ruby-forum.com/t/rails-validations-place-them-in-application-rb/164869/5 "2009-04-07T02:39:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 7, 2009, 9:33am UTC](https://www.ruby-forum.com/t/rails-validations-place-them-in-application-rb/164869/6 "2009-04-07T09:33:51Z")

</div>

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](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 7, 2009, 9:41am UTC](https://www.ruby-forum.com/t/rails-validations-place-them-in-application-rb/164869/7 "2009-04-07T09:41:29Z")

</div>

On Apr 7, 3:18 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto: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
