What code runs when a Rails application is started? - ruby-on-rails

is there an event I can write some code for when a Rails application is first started? I want to make sure my administrator user exists whenever the app starts. I am using devise auth framework in case that helps.

It would be better to make a new user in seeds.rb and run rake db:seed before starting the application - that's what it's for.

You could probably make an initializer inside of config/initializers. You have to make sure you load up devise first, though.

Related

Where do I put a recurring script that updates database from api in rails

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.
I would suggest using a background processor such as Sidekiq: https://github.com/mperham/sidekiq
Once using Sidekiq, you need a scheduler like https://github.com/moove-it/sidekiq-scheduler to make sure it happens periodically as you require.
This will become easier to maintain as your application grows and you need more workers. It also moves your scheduling into version control.

Is the rails console dynamic?

is the console in rails (~ rails c) dynamic? For example; if I open the console and then make changes to a model will it pick these changes up or do I have to exit out of the console and run rails c again for it to pick up the changes in the model?
You will need to call the reload! method in the console to reload the changes. This method's magic is automatically called by rails server in development mode.
As a comment's pointed out beneath and another answer here, if you change things to do with the environment of the application, such as adding new gems to the Gemfile, making changes to anything in config or adding a new plugin then you'll need to restart the console. Any changes to app will be reloadable with reload!
If you were using this particular way to test that a method was working, I wouldn't. Tests (as in, the Test::Unit or RSpec) variants are much nicer because you have a reproducible way of running them again and again. rails console is great for one-off testing, but if you want to write a maintainable application then write tests.

Can I somehow execute my db/seeds.rb file from my rails app?

I am building a demo, and I want to make it very easy for a non-technical person to set up and run the demo. I have built a seeds.rb file with lots of demo data in it. I want to be able to reset the rails app to a known state by providing an administrator-level action via a page link. I don't want to provide these non-tech demonstrators with a command line and rake, because they might shoot themselves in the foot.
I have looked into using load 'db/seeds.rb' within a method, but that doesn't quite do what I want. I know I am missing something, but what?
You can call Rails.application.load_seed. That's all rake db:seed does.
I prefer the classic method:
bundle exec rails db:seed
But I guess, that you can also call Rails.application.load_seed as mentioned.

Is there a way to know that a rails app has been launched from a rake command?

I'm doing a bunch of caching tasks in my rails initializers.
They're useful for the website, but absolutely useless and time consuming when I launch rake tasks.
Is there a way in rails to know when the app has been launched from a rake command?
Thanks
I guess the other option is to ask whether you can tell whether the app has been launched as a website and only run the initializers then.
In that case you should be able to set environment variables in whichever web server you are running the application and you may then be able to trigger your initializer when the environment variable is set. Something like:
if ENV['INITIALIZE_BLAH']
# Do your website only initialization
end

Running DB Migrations from application

I have a rails application where each user has a separate database. (taking Joel Spolsky's advice on this). I want to run DB migrations from the rails application to create a new database and tables for this user.
What is the easiest way to do this?
Maybe the db migration is not the best for this type of thing. Thanks!
It would be nice if it could be a completely automated process. The following process would be ideal.
A user signs up on our site to use this web app
Migrations are run to create this users database and get tables setup correctly
Is there a way of calling a rake task from a ruby application?
To answer part of your question, here's how you'd run a rake task from inside Rails code:
require 'rake'
load 'path/to/task.rake'
Rake::Task['foo:bar:baz'].invoke
Mind you, I have no idea how (or why) you could have one database per user.
We use seperate configuration files for each user. So in the config/ dir we would have roo.database.yml which would connect to my personal database, and I would copy that over the database.yml file that is used by rails.
We were thinking of expanding the rails Rakefile so we could specify the developer as a environment variable, which would then select a specfic datbase configuration, allowing us to only have one database.yml file. We haven't done this though as the above method works well enough.
Actually I have discovered a good way to run DB migrations from an application:
ActiveRecord::Migrator.migrate("db/migrate/")

Resources