MySQL Dumping Cheat-Sheet

Make a quick dump of a mysql database:

mysql_dump db_name > backup-file.sql

You can then read that dump file back into the server like this

mysql db_name < backup-file.sql

or like this:

mysql -e “source /path/to/backup/backup-file.sql” db_name

List of Ruby Operands

operand what it does
<< append (in context of strings, arrays, IO); shift position of bits in Bignum and Fixnum
>> shift position of bits in Bignum and Fixnum
~ bitwise NOT (high-presendence)
& bitwise AND (medium-presendence)
| bitwise OR (medium-presendence)
^ bitwise XOR (medium-presendence)
** exponent
+ addition
* multiplication
/ division
% modulo operator

Tips for Working with Rails

  1. Go to script/console to open ruby in interactive mode. script/console loads your environments (everything in environment.rb and all your plugin and gem initializers), so you basically have access to all your models right from interactive ruby mode (IRB).
  2. Remember use of the p (or its alias puts) command to print stuff to the output buffer. This is useful for testing in IRB mode, inside of a test, or other places where you have an output buffer. If you’re running mogrel, your output goes to the terminal. If you’re running Passenger with Apache, check your apache error_log for the output.
  3. Puts is not to be confused with logger.debug which will log the output of whatever you pass it to your log/development.log file. To see the log file as your are running, use this:
    tail -f log/development.log

    (The -f flag keeps the tail process running and will show you anything that is written to your log file as it is written.)

    You can see lots of info in the development log of course: all the actions that Rails is trying to render as well as all the SQL it is executing. Like I said above, use logger.debug to explicitly send stuff you want to see to this log.

    This only works in controllers:

    logger.debug “the value of myVar is #{myVar}”

    But this works everywhere:
  4. Don’t forget your <em>explicit conversions</em>. These will work on most objects (where it makes sense) and will convert the variable to something that can be read easily:
    to_s  to string
    to_i  to integer
    to_f  to float
    to_a  to array
  5. Use the string concatenator (plus sign), like so:
    puts “myVariable is ” + myVariable.to_s
    logger.debug “the value of myVar is” + myVar
  6. Use inspect or dump to look the contents of a variable command as in

    (You might want to try this):

    p myVar.inspect
  7. The inspect command often will output a long object (as in, lots of output). If you see \n markers (newline) in your output and it all runs together on one line, try “y” before the object to make it more readable. This also shows it in tree-like mode which helps you to see the objects within arrays and stuff. This works well in IRB:

    y myVar

  8. Keep in mind that you can do this stuff inside your tests too. (puts output will be printed by the test runner.) For functional testing, remember the “four Hashes of the apocalypse” – hashes which contain interesting information after your tests have run. Interesting enough to perform asserts against:

      * assigns Any objects that are stored as instance variables in actions for use in views.
      * cookies Any cookies that are set.
      * flash Any objects living in the flash.
      * session Any object living in session variables.

    Also, you can look at the response you’re getting with @response. Also keep in mind:

      * @controller The controller processing the request
      * @request The request
      * @response The response

    I like to use inspect on these, and surround them with big markers so that I can see them clearly when I’m running my test.

    puts “===== RESPONSE====== ”
    puts @response.inspect
    puts “==================== ”


Using Mac ports

sudo port install ImageMagick

If you have an existing ImageMagick installed, you will get this error:

Error: Target org.macports.activate returned: Image error: Another version of this port (ImageMagick @6.4.3-6_0+q16) is already active.

To remove the old version, try:

sudo port uninstall ImageMagick @6.4.3-6_0+q16
—> Deactivating ImageMagick @6.4.3-6_0+q16
—> Uninstalling ImageMagick @6.4.3-6_0+q16


set this in config/initializers/paperclip.rb

Paperclip.options[:image_magick_path] = ‘/opt/local/bin/’

Paperclip with Passenger

1) be sure to put in your environment file (inside your initializer)

config.gem “paperclip”

2) make sure your form_form has multipart set to true like this:

<% form_for @photo, :html => { :multipart => true } do |f| %>

3) if using passenger, you must put this in config/initializers/paperclip.rb:

if RAILS_ENV == “development”
Paperclip.options[:image_magick_path] = ‘/opt/local/bin/’

(Make sure your imagemagick binaries are really in /opt/local/bin/. You can type /opt/local/bin/convert -v to check. I would recommend the MacPorts installation of ImageMagick and re-install if you are unsure.)

4) If you’re having trouble getting it to generate thumbnails, try setting Paperclip.options[:log_commands] = true and see what it’s trying to run.

How do I reload my models if I’m in script/console?

Sometimes you’re in script/console and you make a change to your model files, but since Ruby on reads the models when the environment loads, your change isn’t reflected right away in your script/console environment. This will explicitly tell Ruby to reload the environment from scratch. Unfortunately you’ll loose any local variables which were assigned too.

script/console reload!

Writing to the development log in Rails.

logger.debug WHAT_TO_OUTPUT

***** I think this syntax only works in controllers and outside of a controller you have to use rails_default_debugger ******

(where WHAT_TO_OUTPUT is a string or interpolated to be a string)

The log is in your rails app: log/development.log

Methods ending with an equal sign? (Ruby basics)

A method defined on a model that ends with = is an assignment method, so when ActiveRecord is trying to save an attribute to this model, you can define a custom action to perform the save. Useful, for example, when passing a hash to this model for some list of things that is not part of active record.

With Passenger where does the console log (output from puts) go?

When running Passenger (module for running Rails on Apache), the console log (output from puts) goes into the apache error log (see /var/log/apache2/error_log)