EzDevInfo.com

rake

A make-like build utility for Ruby. Rake -- Ruby Make

Rails DB Migration - How To Drop a Table?

I added a table that I thought I was going to need, but now no longer plan on using it. How should I remove that table?

I've already ran migrations, so the table is in my database. I figure rails generate migration should be able to handle this, but I haven't figured out how yet.

I've tried rails generate migration drop_tablename, but that just generated an empty migration.

What is the "official" way to drop a table in Rails?


Source: (StackOverflow)

puts vs logger in rails rake tasks

In a rake task if I use puts command then I see the output on console. However I will not see that message in log file when app is deployed on production.

However if I say Rails.logger.info then in development mode I see nothing on console. I need to go to log file and tail that.

I would ideally like to use Rails.logger.info and in development mode inside the rake task, the output from logger should also be sent to console.

Is there a way to achieve that?


Source: (StackOverflow)

Advertisements

including rake tasks in gems

1) Is there a 'best' place for rake tasks inside of gems? I've seen them in /tasks, /lib/tasks, and I've seen them written as *.rb and *.rake -- not sure which (if any) is 'correct'

2) How do I make them available to the app once the gem is configured in the environment?


Source: (StackOverflow)

How to rollback just one step using rake db:migrate

After adding migration files in the db/migrate folder and running rake db:migrate, I want get back to the previous step, I think using VERSION=n is the right way to do that, but I don't know the correct value of n to use. Is there any command to check the current n value?

It would be great if anyone could provide full instructions on how to use rake db:migrate.


Source: (StackOverflow)

I have a Rails task: should I use script/runner or rake?

For ad hoc Rails tasks we have a few implementation alternatives, chief among which would seem to be:

script/runner some_useful_thing

and:

rake some:other_useful_thing

Which option should I prefer? If there's a clear favourite then when, if ever, should I consider using the other? If never, then why would you suppose it's still present in the framework without deprecation warnings?


Source: (StackOverflow)

How do I use "gets" on a rake task?

I get an error whenever I try to use the function gets within a rake task. Is there a way to make it work?

The error says, "no such file or directory - (rake task name)"


Source: (StackOverflow)

Why is rake db:migrate:reset not listed in rake -T?

Why are some rake tasks not listed by rake -T? Like db:migrate:reset? I can execute it without a problem, but why is it not listed there? Is there a way to get a real full list of rake tasks?

% rake -T
(in /home/zeus/projects/my_project)
rake about              # List versions of all Rails frameworks and the environment
rake db:create          # Create the database from config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
rake db:drop            # Drops the database for the current Rails.env (use db:drop:all to drop all databases)
rake db:fixtures:load   # Load fixtures into the current environment's database.
rake db:migrate         # Migrate the database (options: VERSION=x, VERBOSE=false).
rake db:migrate:status  # Display status of migrations
rake db:rollback        # Rolls the schema back to the previous version (specify steps w/ STEP=n).
rake db:schema:dump     # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load     # Load a schema.rb file into the database
rake db:seed            # Load the seed data from db/seeds.rb
rake db:setup           # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)
rake db:structure:dump  # Dump the database structure to an SQL file
rake db:version         # Retrieves the current schema version number
rake doc:app            # Generate docs for the app -- also availble doc:rails, doc:guides, doc:plugins (options: TEMPLATE=/rdoc-template.rb, TITLE="Custom Title")
rake erd                # Generate an Entity-Relationship Diagram based on your models
rake log:clear          # Truncates all *.log files in log/ to zero bytes
rake middleware         # Prints out your Rack middleware stack
rake notes              # Enumerate all annotations (use notes:optimize, :fixme, :todo for focus)
rake notes:custom       # Enumerate a custom annotation, specify with ANNOTATION=CUSTOM
rake rails:template     # Applies the template supplied by LOCATION=/path/to/template
rake rails:update       # Update both configs and public/javascripts from Rails (or use just update:javascripts or update:configs)
rake routes             # Print out all defined routes in match order, with names.
rake secret             # Generate a crytographically secure secret key (this is typically used to generate a secret for cookie sessions).
rake spec               # Run all specs in spec directory (excluding plugin specs)
rake spec:acceptance    # Run the code examples in spec/acceptance
rake spec:controllers   # Run the code examples in spec/controllers
rake spec:helpers       # Run the code examples in spec/helpers
rake spec:lib           # Run the code examples in spec/lib
rake spec:mailers       # Run the code examples in spec/mailers
rake spec:models        # Run the code examples in spec/models
rake spec:rcov          # Run all specs with rcov
rake spec:requests      # Run the code examples in spec/requests
rake spec:routing       # Run the code examples in spec/routing
rake spec:views         # Run the code examples in spec/views
rake stats              # Report code statistics (KLOCs, etc) from the application
rake test               # Runs test:units, test:functionals, test:integration together (also available: test:benchmark, test:profile, test:plugins)
rake test:recent        # Run tests for recenttest:prepare / Test recent changes
rake test:uncommitted   # Run tests for uncommittedtest:prepare / Test changes since last checkin (only Subversion and Git)
rake time:zones:all     # Displays all time zones, also available: time:zones:us, time:zones:local -- filter with OFFSET parameter, e.g., OFFSET=-6
rake tmp:clear          # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tmp:sockets:clear)
rake tmp:create         # Creates tmp directories for sessions, cache, sockets, and pids

Source: (StackOverflow)

Running ruby debug in rspec?

I'm trying to get ruby debugger running in one of my specs:

describe User do
  it "should be valid" do
    debugger
    User.new.should be_valid
  end
end

When I run rspec though, I get:

* debugger statement ignored, use -d or --debug option to enable debugging

I've tried the following:

rake spec --debug
rake spec --debug  --trace
rake spec:models --debug
bundle exec rspec --debug
bundle exec rspec --debug spec/models/
bundle exec rspec --d spec/models/
bundle exec "rspec --debug" spec/models/
bundle exec rspec --debugger spec/models/
bundle exec --debugger rspec spec/models/
bundle --debugger exec rspec spec/models/
bundle --debugger exec rspec spec/models/
bundle exec --debugger rspec spec/models/
bundle exec rspec --debugger spec/models/

Any ideas on how to exec rspec in the right way? I'm on Rails 3.0.5, ruby 1.9.2, rspec 2.5.1, ruby-debug19.

Thanks, Justin


Source: (StackOverflow)

Rails and PostgreSQL: Role postgres does not exist

I have installed PostgreSQL on my Mac OS Lion, and am working on a rails app. I use RVM to keep everything separate from my other Rails apps.

For some reason when I try to migrate the db for the first time rake cannot find the postgres user. I get the error

 FATAL:  role "postgres" does not exist

I have pgAdmin so I can clearly see there is a postgres user in the DB - the admin account in fact - so I'm not sure what else to do.

I read somewhere about people having issues with PostgreSQL because of which path it was installed in, but then I don't think I would have gotten that far if it couldn't find the db.


Source: (StackOverflow)

Global access to Rake DSL methods is deprecated

I am working through the Ruby on Rails 3 tutorial book and typed the following on the command line:

rake db:migrate

which produced the following warning.

WARNING: Global access to Rake DSL methods is deprecated.  Please Include
    ...  Rake::DSL into classes and modules which use the Rake DSL methods.

WARNING: DSL method DemoApp::Application#task called at /Users/imac/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/railties-3.0.7/lib/rails/application.rb:215:in `initialize_tasks'

I am not sure what to do about it or how to work with it. I don't know any other command for Rake.

How can I fix this problem?


Source: (StackOverflow)

How to fix the uninitialized constant Rake::DSL problem on Heroku?

I am getting errors similar to the ones in these questions, except mine are occuring on Heroku:

2011-05-30T09:03:29+00:00 heroku[worker.1]: Starting process with command: `rake jobs:work`
2011-05-30T09:03:30+00:00 app[worker.1]: (in /app)
2011-05-30T09:03:30+00:00 heroku[worker.1]: State changed from starting to up
2011-05-30T09:03:33+00:00 app[worker.1]: rake aborted!
2011-05-30T09:03:33+00:00 app[worker.1]: uninitialized constant Rake::DSL
2011-05-30T09:03:33+00:00 app[worker.1]: /app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.0/lib/rake/tasklib.rb:8:in `<class:TaskLib>'

The answer in those questions seems to be to specify gem 'rake', '0.8.7' because the 0.9 version causes the problem.

When I try to add gem 'rake', '0.8.7' to my gemfile and push to Heroku I get this error:

Unresolved dependencies detected; Installing...
You have modified your Gemfile in development but did not check
the resulting snapshot (Gemfile.lock) into version control

You have added to the Gemfile:
* rake (= 0.8.7)
FAILED: http://devcenter.heroku.com/articles/bundler
! Heroku push rejected, failed to install gems via Bundler
error: hooks/pre-receive exited with error code 1
To git@heroku.com:my_app.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@heroku.com:my_app.git'

My gemfile normally works fine on Heroku. What should I do?


Source: (StackOverflow)

Rails Rake: How to pass in arguments to a task with :environment

So I am able to pass in arguments as follows

desc "Testing args"
task: :hello, :user, :message do |t, args|
  args.with_defaults(:message => "Thanks for logging on")
  puts "Hello #{args[:user]}. #{:message}"
end

I am also able to load the current environment for a Rails application

desc "Testing environment"
task: :hello => :environment do 
  puts "Hello #{User.first.name}."
end

What I would like to do is be able to have variables and environment

desc "Testing environment and variables"
task: :hello => :environment, :message do |t, args|
  args.with_defaults(:message => "Thanks for logging on")
  puts "Hello #{User.first.name}. #{:message}"
end

But that is not a valid task call. Does anyone know how I can achieve this?


Source: (StackOverflow)

How do I return early from a rake task?

I have a rake task where I do some checks at the beginning, if one of the checks fails I would like to return early from the rake task, I don't want to execute any of the remaining code.

I thought the solution would be to place a return where I wanted to return from the code but I get the following error

unexpected return

Source: (StackOverflow)

Undefined method 'task' using Rake 0.9.0

I just updated Rake to the latest version (0.9.0.beta.4) and the rake command ends up with the following error message:

rake aborted!
undefined method `task' for #<Anelis::Application:0x9223b6c>

Here is the trace:

undefined method `task' for #<Anelis::Application:0x97ef80c>
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:214:in `initialize_tasks'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:139:in `load_tasks'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
/home/amokrane/Documents/prog/web/learning_rails/anelis/Rakefile:7:in `load_string'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/environment.rb:28:in `eval'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/environment.rb:28:in `load_string'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/environment.rb:16:in `load_rakefile'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/application.rb:495:in `raw_load_rakefile'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/application.rb:78:in `block in load_rakefile'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/application.rb:129:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/application.rb:77:in `load_rakefile'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/application.rb:61:in `block in run'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/application.rb:129:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/application.rb:59:in `run'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/bin/rake:31:in `<top (required)>'
/usr/local/rvm/gems/ruby-1.9.2-p136/bin/rake:19:in `load'
/usr/local/rvm/gems/ruby-1.9.2-p136/bin/rake:19:in `<main>'

Anyone experienced the same issue? What could possibly be wrong? Note that I am running Rails 3.0.3, you may also be interested in the content of my Gemfile:

source 'http://rubygems.org'
gem 'rails', '3.0.3'
gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'mysql2'
gem 'legacy_data'
gem 'resources_controller', :git => 'git://github.com/ianwhite/resources_controller'
gem 'will_paginate', '3.0.pre' # pagination
gem 'jquery-rails', '>= 0.2.6'
gem "rmagick" # sudo aptitude install libmagick9-dev
gem "paperclip", "~> 2.3"
gem "nested_form", :git => "git://github.com/madebydna/nested_form.git"
gem "meta_search"
gem "hirb"
gem "devise"
gem "rails_admin", :git => "git://github.com/sferik/rails_admin.git"

How can I fix this problem?


Source: (StackOverflow)

Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative?

The latest changesets to Ruby 1.9.2 no longer make the current directory . part of your LOAD_PATH. I have a non-trivial number of Rakefiles that assume that . is part of the LOAD_PATH, so this broke them (they reported "no such file to load" for all require statements that based off the project path). Was there a particular justification for doing this?

As for a fix, adding $: << "." everywhere works, but seems incredibly hacky and I don't want to do that. What's the preferred way to make my Rakefiles 1.9.2+ compatible?


Source: (StackOverflow)