I want to put my chargify conf inside the initializer,
but I found the initializer won't execute in my rails c, is there a way to invoke my initializers so I can test in my console?
Chargify.configure do |c|
c.api_key = "keykey"
c.subdomain = "test-site"
end
The config/initializers will execute, but only once, on initial load. So if you're making changes to config/initializers while the console is running you won't see the results of those changes happening.
Your best option is to stop and restart rails c, or you can type the reload! command in the console.
Also, if you are using spring that will sometimes prevent changed initializers from reloading. in that case do spring stop before you restart the console.
Yes, every .rb file in config/initializers is run whenever you run the console, run a rake task, or run your tests. Additonally, the environment configuration (config/environments) is run before the initializers.
Apparently not anymore unless you disable spring:
export DISABLE_SPRING=1
Related
This question already has answers here:
Is there a Rails Console command (Rails 3+) to reload changed code?
(3 answers)
Closed 7 years ago.
I was using the Rails Console to test a few things by hand and found that one of my initializers (under config/initializers) had an error. I exited the console, made the change, and ran "rails c" again. Much to my surprise, the updated initializer was not executed.
Here is what I found:
-- If I start a new Bash session and enter "rails c" all the initializers get executed.
-- If I add / change an initializer and then re-run "rails c" within the same Bash session, the initializers are not executed.
This occurs under development which disables caching.
What the heck is going on? Thanks.
The introduction of Spring was meant to make loading your rails app faster, by keeping a copy of the app in memory and only reloading what changes.
Unfortunately, sometimes your initializers can stay exactly the same, but they need to be re-run. An example is the "business_time" gem, which generates an initializer that loads a YAML file. You can change the YAML file and restart console, but Spring sees that your initializer hasn't changed, so it doesn't re-run it. Thus, the changes to your YAML file go unnoticed, This is a very difficult error to troubleshoot.
If you're having initializer problems, then exit out of console and run:
bundle exec spring stop
before going back into console. This will force a fresh reload of the initializers.
You can also disable Spring entirely by removing the gem from your Gemfile.
Type this command into rails
reload!
It's reload your rails c
I want to execute a rake task when the server of my application starts.
In config/application.rb i put the following:
if !Rails.env.production?
Rake::Task[ "init:db_records" ].invoke
end
The rake task is well defined, and runs without a problem if i invode it from terminal
rake init:db_records
But when placed in config/application.rb (or even in any initializers/*) i got the following error.
Don't know how to build task 'init:db_records'
What is the way to execute a rake task when the server starts ?
Thanks!
Rails already has a mechanism for setting up a development database -- rake db:seed. It does not run automatically when you start the app, but it does run as part of rake db:setup.
Unless you have a good reason, it's usually best to stick the conventions that Rails provides.
For those who encounter the same problem in the future.
I achieved this by creating a new file in the initializers directory, where i put the code of the rake task.
The advantage of this at this point, is that the application is already loaded, so you have access to ActiveRecord functions...
Putting the code directly in config/application.rb didn't work, since my models were not loaded yet.
Hope it will help!
Your Rake tasks are (likely) defined in a Rakefile. The initializer has no idea that file even exists, so it doesn't know about the tasks within.
The easiest way to circumvent this is by doing something like this:
Dir.chdir(Rails.root) do
`rake init:db_records`
end
That is, change the working directory to the root rails directory, then running the command.
I need to run a few commands whenever I start my Rails console, like setting up the logger, or to set the time to my time zone.
Right now I'm copying and pasting these commands after Rails is started. Can I write a script to make these commands run automatically after IRB is started?
Rails' console is IRB. IRB supports an .irbrc file, which contains initialization information and settings.
Read "https://stackoverflow.com/questions/123494/whats-your-favourite-irb-trick" and "My .irbrc for console/irb" for ideas.
I wrote an extended answer to this in another question but the short answer is that if you are using Rails 3 or above you can use the console method on YourApp::Application to make it happen:
module YourApp
class Application < Rails::Application
...
console do
Time.zone = A.info.time_zone
end
end
end
How do you write a rake task outside the Rails application directory and make it run.
For example, say write a sample rake task
task :dummy do
puts User.first.first_name
end
this task is under ~/fun/sample.rake
And my rails application is located at ~/my_app/
Now I need to run the sample.rake. I know i need to load the environment, DB etc., etc., how do i do that? Stuck at this for the past hour.
I tried the one below, obviously it did not work because it did not know how to build it.
rake -f ~/my_app/Rakefile dummy
Note: I should not touch the files inside the Rails application but I can write whatever I want inside the fun directory
You need to create a Rakefile and load the rake gem. See this answer: Ruby: Accessing rake task from a gem without Rails
eg: I wanna run something automatically when start the server
I get a not so clear view about that, is there something about rake?
...
I do remove the stupid example
You could create an initializer file in config/initializers folder for example task_scheduler.rb and then use Rufus scheduler to run tasks:
scheduler = Rufus::Scheduler.start_new
scheduler.in '4s' do
autocallprocess_method
end
scheduler.every '1m' do
autocallprocess_method
end
So you want to run a piece of code that in initialises something on server startup, rather than as a rake task/controller action etc? Simplest way is to create a file in config/initializers and put any ruby code in there.
Any file in this folder gets run on server startup.