Blog
-
October 14, 2011 @ 2:58am
Default Values for Associations in Rails
Sometimes in a Rails app, you'd like to have a default value for an association. Consider the following example from a Stack Overflow question:
class User < ActiveRecord::Base has_one :profile end class Profile < ActiveRecord::Base belongs_to :user end user = User.new user.profile.something #=> ERRORRails doesn't provide a built-in way to specify a default value, but we can do it ourselves:
class User < ActiveRecord::Base has_one :profile def profile_with_default profile_without_default || build_profile end alias_method_chain :profile, :default end class Profile < ActiveRecord::Base belongs_to :user end user = User.new user.profile.something #=> builds a profile for you -
February 9, 2011 @ 6:50am
Filter in the Database, not the Application
Code smells are patterns in code that can indicate possible problem areas. One code smell is loading many records out of a database when you only want some of them and doing filtering in the application logic.
The other day, I sniffed out this smell in our suggested users feature. We assign each suggested user a weight that determines their probability of being suggested. A user with a weight that is double another user's has double the odds of being suggested.
To determine what users to suggest, we were loading all suggested users out of the database and using an algorithm that repeatedly picked users based on a weighted probability until it had the desired amount.
-
May 1, 2010 @ 11:48pm
Testing AJAX with Cucumber and Capybara
For a recent project, I used Cucumber and Capybara for my user stories. When I decided to AJAXify the functionality, I wrote some stories tagged with @javascript that were nearly identical to my non-javascript versions (non-JS and JS). Since the changes to the page content should be the same with or without Javascript, testing the page content wouldn't actually verify that the AJAX funtionality was working. To solve this, I created the step "Then the page should not change" with the following definition:
Then 'the page should not change again' do evaluate_script('window.onunload = window.stop') endNow, if the user clicks a link that doesn't have its behavior overwritten by Javascript, a blank page will be displayed and any following steps will fail.
-
January 29, 2010 @ 7:47am
Early Returns for Clarity's Sake
I don't like to nitpick about programming style, but there is one thing that always bugs me. I frequently come across functions structured as follows:
function foo() { if (a) { // do stuff } else { return false; } }I always wonder why the programmer didn't use an early return:
function foo() { if (!a) return false; // do stuff } -
October 28, 2009 @ 5:41pm
Analogy for Net Neutrality
ISPs are like UPS or Fedex to the web. They deliver packets from sender to receiver.
With net neutrality, they must place equal priority on each delivery and deliver the packets untampered.
Without, they may open, inspect, modify, delay, or even halt packets of their choice.
-
May 25, 2009 @ 10:13am
Using a Float Column in a Where Clause
I spent about 12 hours today trying to figure out why one of my
validates_uniqueness_ofconstraints was not working. Here's why:mysql> create table some_table (some_float_column float); Query OK, 0 rows affected (0.01 sec) mysql> insert into some_table (some_float_column) values (8.4537); Query OK, 1 row affected (0.00 sec) mysql> select * from some_table where some_float_column = 8.4537; Empty set (0.01 sec)Notice how I'd just inserted a row with that value yet the row was not returned. All I had to do was change the float column to a decimal column.
-
March 22, 2009 @ 6:22pm
Auto-Start Juggernaut with Cucumber
If you use Juggernaut and Cucumber, many of your features can fail unless you have a Juggernaut server running. You can add the following to your features/support/env.rb to automatically start and stop Juggernaut when you run your features:
juggernaut = fork { exec 'juggernaut -c config/juggernaut.yml' } at_exit { exec 'kill', juggernaut.to_s }