Filed under: rails

Be careful updating Rails' ActiveRecord Model attributes in-place

I just spent a couple minutes debugging a simple method that was updating a string in my database.

The column in the database was initialized to empty string.  I had a method which would pull that record from the database, reinitialize the value to empty string, and then append to it using the << operator.  However, after calling save!, the record was not updated.  This had me grumbling, "Why isn't ActiveRecord updating my attribute!"

It was due to my misunderstanding of how the ActiveRecord Dirty flag works in cases of in place attribute updating.

Below is an example that shows how this works and the use of the <attribute_name>_will_change! method:

For more info, checkout:

Testing Stalker (Beanstalkd) Jobs with RSpec in Rails

Stalker https://github.com/han/stalker is a nice gem for running Beanstalkd background jobs in Rails.

But it took me a bit of Google-ing to figure out how to write tests for those jobs.  Here's what I learned:

Below is an example of a "basic_job" that puts "second_job" on the queue when run:

And here's the corresponding RSpec test

Note that the Stalker jobs file was wrapped with "module Stalker".  This is necesseary so that when we load the jobs file in our RSpec test the job method is called in the correct Object space.

Note that you'll need to install pgrep on OS X for the precondition check to run.  You can do that with Homebrew, see Best Unix Shell Tools

 

Check out the Stalker source code to see all available methods of Stalker at your disposal:  https://github.com/han/stalker/blob/master/lib/stalker.rb

For example:

  • prep:  Ensures all handlers and beanstalk-client are setup correctly
  • work_one_job:  Works a single job in the queue

 

References:

 

Setting Up Evergreen in a Rails 3.2 application to unit test your coffeescript Backbone.js models

Evergreen (https://github.com/jnicklas) is a nice wrapper for the Jasmine javascript testing framework, and it's very easy to use in a Rails app to test Backbone.js models.  (Note: I'm using https://github.com/codebrew/backbone-rails as well.)

Simply require the gem in your Gemfile's development/test environments:

And add an Evergreen configuration file: evergreen.rb in your config directory:

Then create a folder in your project: /spec/javascripts

You can add a spec_helper.coffee file here that will automatically loaded, in which you can require the necessary javascript files from the asset path, e.g.:

Then write your model tests:

Then just start your rails app, and browse to http://localhost:3000/evergreen to see the results of your tests, or run rake spec:javascripts

Generating a Trample configuration file from a Rails server log

Below is a Rake Task to parse a Rails server log using Timber to generate a Trample configuration file for load testing.

Just download Timber from https://github.com/cainlevy/timber and copy it into your rails app's: /vender/plugins/cainlevy-timber

Then copy this task into: /lib/tasks

Then run with: rake trample:generate LOG=log/development.log"

(Don't forget to convert any filtered parameters like passwords from [FILTERED] to the actual value.)

Generating httperf wsesslog data from a Rails server log

Below is a Rake Task to parse a Rails server log using Timber to generate httperf wsesslog formatted data for load testing.

Just download Timber from https://github.com/cainlevy/timber and copy it into your rails app's: /vender/plugins/cainlevy-timber

Then copy this task into: /lib/tasks

Then run with: rake wsesslog:generate LOG=log/development.log"

(Don't forget to convert any filtered parameters like passwords from [FILTERED] to the actual value.)