Do rails rake tasks provide access to ActiveRecord models?
Figured it out, the task should look like: namespace :test do task :new_task => :environment do puts Parent.all.inspect end end Notice the => :environment dependency added to the task
Figured it out, the task should look like: namespace :test do task :new_task => :environment do puts Parent.all.inspect end end Notice the => :environment dependency added to the task
A Rake task is basically a block. A block, except lambdas, doesn’t support return but you can skip to the next statement using next which in a rake task has the same effect of using return in a method. task :foo do puts “printed” next puts “never printed” end Or you can move the code … Read more
I’ve used the extremely popular Whenever on projects that rely heavily on scheduled tasks, and it’s great. It gives you a nice DSL to define your scheduled tasks instead of having to deal with crontab format. From the README: Whenever is a Ruby gem that provides a clear syntax for writing and deploying cron jobs. … Read more
bundle exec is a Bundler command to execute a script in the context of the current bundle (the one from your directory’s Gemfile). rake db:migrate is the script where db is the namespace and migrate is the task name defined. So bundle exec rake db:migrate executes the rake script with the command db:migrate in the … Read more
If you need the task to behave as a method, how about using an actual method? task :build => [:some_other_tasks] do build end task :build_all do [:debug, :release].each { |t| build t } end def build(type = :debug) # … end If you’d rather stick to rake‘s idioms, here are your possibilities, compiled from past … Read more
I know two ways to do this: This will reset your database and reload your current schema with all: rake db:reset db:migrate This will destroy your db and then create it and then migrate your current schema: rake db:drop db:create db:migrate All data will be lost in both scenarios.
db:migrate runs (single) migrations that have not run yet. db:create creates the database db:drop deletes the database db:schema:load creates tables and columns within the existing database following schema.rb. This will delete existing data. db:setup does db:create, db:schema:load, db:seed db:reset does db:drop, db:setup db:migrate:reset does db:drop, db:create, db:migrate Typically, you would use db:migrate after having made … Read more
You can specify formal arguments in rake by adding symbol arguments to the task call. For example: require ‘rake’ task :my_task, [:arg1, :arg2] do |t, args| puts “Args were: #{args} of class #{args.class}” puts “arg1 was: ‘#{args[:arg1]}’ of class #{args[:arg1].class}” puts “arg2 was: ‘#{args[:arg2]}’ of class #{args[:arg2].class}” end task :invoke_my_task do Rake.application.invoke_task(“my_task[1, 2]”) end # … Read more