Testing

  • rake db:test:clone Recreate the test database from the current environment’s database schema
  • ruby script/runner “User.to_fixture” To create a yaml fixture from the database
  • ruby test\functional\events_controller_test.rb -n test_conflict To run one test.

Updating the database

  • rake db_schema_dump run after you create a model to capture the schema.rb
  • rake db_schema_import import the schema file into the current database (on error, check if your schema.rb has ?:force => true? on the create table statements
  • ruby script\generate migration MigrationName generate a new migration with a new highest version
  • rake migrate migrate your current database to the most recent version
  • rake environment RAILS_ENV=production migrate to migrate the production database

MySql Problem

On one computer, Ruby was installed in /usr/local/lib but MySql was in /usr/lib. So running “gem install mysql” gave an error. Here is the command used:

gem install mysql -- --with-mysql-config=/usr/lib/mysql/mysql_config

Fixing the debug prompt

The normal debug prompt shows the object that you are currently in. Normally, this is fine but sometimes when you are debugging an HTML or RedCloth object, it can be very long. To get back to a normal debug prompt use:

conf.prompt_mode = :SIMPLE

However, this needs to be typed in every time, which is a pain. So a better solution is to create an .irbrc file in the Rails root that contains:

IRB.conf[:PROMPT_MODE] = :SIMPLE

Firing a controller’s action from the console

http://www.bigbold.com/snippets/posts/show/600

ruby script/console

irb> require 'action_controller/test_process'
irb> require 'application'
irb> require 'site_controller'
irb> request = ActionController::TestRequest.new
irb> response = ActionController::TestResponse.new
irb> request.env['REQUEST_METHOD'] = 'GET'
irb> request.action = "late_employee" 
irb> InfoController.process(request,response)

Pruning FastCGI processes

FastCGI on Apache tends to create zombie processes. To keep these pruned, add this line to a cron job:

pkill -9 -u `whoami` -f dispatch.fcgi

Here is a longer way to do this with a Ruby script:

#!/usr/bin/ruby
#Modified from Julik's original code posted to TextDrive forums.

def kill_fcgi_proc(line)
  its = line.strip.split(/\s+/)
  pid = its[0]
  puts "KILLING #{line}" 
  `kill -9 #{pid}`
  sleep(3)
end

pstab = `ps ww -u maxdunnc`

pstab.scan(/^.*dispatch\.fcgi\s*$/) do |line|
  kill_fcgi_proc line
end

Debugging Views in Development

<%= javascript_include_tag 'prototype' %>

<% if ENV['RAILS_ENV'] == 'development' %>
    <div id="debug" style="margin: 40px 5px 5px 5px;">
        <a href="#" onclick="Element.toggle('debug_info');return false" style="text-decoration: none; color: #ccc;">Show Debug Info ➲</a>
        <div id="debug_info" style="display : none;">
            <%= debug session %>
            <%= debug params %>
        </div>
    </div>
<% end %>

Console Tricks

Cleaning up YAML dumps with sed

Somehow we got a bunch of weird UTF escape sequences in many web pages. To clean them up, use:

sed 's/\\x..//g' infile >outfile