script in rails

You can create a rake task for that.

http://railscasts.com/episodes/66