I'm trying to write a Rake task that will retrieve data stored in the databases of two different Rails apps, with different Ruby and gem versions. What I have so far is
task :get_data do
puts `/<path>/<to>/<first>/<app>/bin/rails runner 'FirstDataRetriever.new.as_set'`
puts `/<path>/<to>/<second>/<app>/bin/rails runner 'SecondDataRetriever.new.as_set'`
end
The problem is that Rails runner is trying to execute the DataRetriever classes using the Ruby version of the Rakefile, which is in a separate repo using different infrastructure.
Both the DataRetriever classes make use of ActiveRecord models in each app, so they must be run in the Rails environment of each app, but I'm not sure how to go about this.
You can specify Ruby version in .ruby-version file inside of root directory of your projects if you are using RVM or rbenv.
You can specify Rails environment use RAILS_ENV environment variable i.e. RAILS_ENV=production bin/rails runner ...
I hope it can help to solve your problems.
Related
Im following a rails tutorial, and when im supposed to run the command: 'bin/rails generate model Article'. An error occurs saying that there isnt such a command.
I'm using 'command prompt with ruby on rails' and in the rails project i can find a Bin folder. Am also using windows 7.
Also What is the difference between running only 'rails generate' instead of running 'bin/rails generate'?
Using rails generate is fine if you have no bin stubs (binaries in the root /bin folder of your project). If you do have bin stubs then it's preferred to use them because they may do additional things specific to your project. But even then, it's (probably) fine to just use rails generate still. The other bin stubs may be a little more necessary to use, though (again, if present) because they tend to be shortcuts to e.g. bundle exec rake.
Rails 4.1 ships with bin stubs. That is, when you generate a Rails 4.1 project it generates bin stubs for you. So this is probably why your tutorial mentioned using them -- they're now there by default. But if you're on an older version of Rails that won't help you much.
The big reason Rails 4.1 includes bin stubs is because Rails uses spring by default now. Spring is an application preloader... that makes it so that when you call e.g. bin/rake ... it will load and keep a running rails environment in the background and then, the 2nd time you call bin/rake it will fork from the running environment giving you almost instantaneous response. So this is an example of "additional things specific to your project" that you get from using bin/rake over just rake and bin/rails over just rails.
Examples:
"Vendoring Rails application at test/dummy"
"What is the recommended way of vendoring Rails for production?"
Vendoring is the moving of all 3rd party items such as plugins, gems and even rails into the /vendor directory.
This is one method for ensuring
that all files are deployed to the production server the same as the dev environment.
Best way to do this is either:
rake gems:unpack
Or
rake rails:freeze:gems
Use rake -T to see a full list of rake tasks.
I try to grasp the logic behind some tasks being "rails" whereas others, the majority, is found as rake task. Why rails server and not rake server for example?
I can understand that the bootstrapping cannot be done in rake: after all, you first need a rakefile and other requirements before you can start using rake. So creating the project with a rails binary seems only practical.
But why generate, server, console, yet not migrate or assets? I don't see the logic. Is there any?
IMO the rails scripts are for "live" console usage, like during development.
The rake tasks are more "automated" tasks, for example, that might be run as part of a build or deploy cycle, like by a CI server. Some rake tasks might group rails/etc. commands together (like tests).
A rake script is a utility/build tool for some common tasks when developing. For example, you need to do deployment, run test, database stuffs, truncate log files, compile assets .... You can create your own custom rake scripts.
A rails script is ruby file located under script directory for the purpose of the gem rails. This is what the gem does. Rails is a ruby web framework, so the command rails is for starting the rails apps, go to rails console, generate files. It's bundled when you install the gem.
You can think of rails command like bundle command for bundler. bundle install, bundle update ... all are related to resolving gem dependencies. rspec command for running tests...
Some gems has an executable script such as rails, bundler, capistrano, whenever, rspec. Some other gems doesn't have such as builder, will_paginate....
You can check this out for how to add executable to a gem, http://guides.rubygems.org/make-your-own-gem/#adding-an-executable
my tool is working on rails 3 , but I have old application are running on 2.1.1 (rails 2.1.1 and ruby 1.8.6) and so many libraries are running on old environment(rails 2.1.1 and ruby 1.8.6). I have to send request from new env to old env to generate report . what is best way to approach this scenario? or how to pass rake task to run on old environment from new env?
the report is under lib/abc.rb (on old environment(rails 2.1.1 and ruby 1.8.6)) call this library and dump the yaml into db from new environment (rails 3 and ruby 1.9.2
Does your Rails 2 application use Bundler? If so, try using
bundle exec <your command>
Ex:
bundle exec rake db:migrate
But standard way is to use rvm (http://beginrescueend.com/) to handle multiple ruby versions and get versions. You can do it easily via 'getsets'.
The following is a quick note I wrote on managing multiple Ruby/gems:
http://keepthingssimple.tumblr.com/post/11274588229/using-rvm-to-keep-things-simple
I have used Warble to make .war files. That worked pretty well.
Some of the tutorials online suggest using the "rake" command at various times. If rake is for compilation, I thought Ruby didn't need compilation. Is it a substitute for Warble? Or do these two play different functions?
When is rake meant to be used?
Rake is a tool written in Ruby for automating tasks, which you also write using a Ruby syntax. Ruby program's don't have to be built, but there are still plenty of other tasks involved in development that you can automate instead of doing yourself each time.
Some examples from Rails include migrating your database to a new schema or creating a new database.
Rake lets you write tasks with a Ruby syntax, and you can also specify dependencies between tasks, so that running one task will cause all of its dependencies to be ran first.
Think of rake as a make for Ruby. For example for one of the gems I develop, the Rakefile includes several tasks, like running all the tests (rake test) or building the gem (rake gem:build). More info on the web site:
http://rake.rubyforge.org/