I have a Rails app set up with a model Account that should be updated every morning with data coming from an external API I'm calling (a CRM). Basically either I create new accounts in my app that I find in the CRM and some of the fields that are mapped with my columns, either I find the account if it already exists and I update it.
So far, I've been putting this code into the seeds.rb
file and from Heroku, where the app is hosted, I set up a scheduler with the command : rails db:seed
that runs periodically.
My issue is that I'm sure there is a better way of doing this. I've read about rake tasks but I did not quite understand how that applied to my case. Otherwise I thought of putting my method in the models/account.rb
file as a self method. But I don't really know how I can invoke it in a rake command to allow me to set up a scheduler in Heroku.
Any idea on where would be the best place to put this method, and how to call it from command line?
Thanks in advance.
You can create a script
directory in your project, and put your script from db/seeds.rb
into this directory, maybe called update_accounts.rb
. Then you can run it with
rails runner script/update_accounts.rb
and schedule that task in heroku. More info about rails runner here.