Trantor Liu
Trantor Liu

Reputation: 9126

Rails: Where to put script/function that runs only when starting server?

I am using the Predictor gem for a recommendation system. I want to write a script to initialize the recommender when running rails server. If I put the script into the initializers/dirctory, it will also be run whenever rake is executed.

Is there a way to add scripts are executed only when running rails server?

Upvotes: 1

Views: 161

Answers (1)

Raj
Raj

Reputation: 22926

Thought it is not recommended, you could update the file bin/rails:

#!/usr/bin/env ruby

puts "Write your custom code here"

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require_relative '../config/boot'
require 'rails/commands'

This runs whenever rails s, rails c or any rails command is called.

Upvotes: 1

Related Questions