Login

ActiveRecord uniqueness checks are 'inherently prone to race conditions"

Using this validation method in conjunction with ActiveRecord::Base#save does not guarantee the absence of duplicate record insertions, because uniqueness checks on the application level are inherently prone to race conditions. For example, suppose that two users try to post a Comment at the same time, and a Comment‘s title must be unique. At the database-level, the actions performed by these users could be interleaved in the following manner:

User 1                 |               User 2
 ------------------------------------+--------------------------------------
 # User 1 checks whether there's     |
 # already a comment with the title  |
 # 'My Post'. This is not the case.  |
 SELECT * FROM comments              |
 WHERE title = 'My Post'             |
                                     |
                                     | # User 2 does the same thing and also
                                     | # infers that his title is unique.
                                     | SELECT * FROM comments
                                     | WHERE title = 'My Post'
                                     |
 # User 1 inserts his comment.       |
 INSERT INTO comments                |
 (title, content) VALUES             |
 ('My Post', 'hi!')                  |
                                     |
                                     | # User 2 does the same thing.
                                     | INSERT INTO comments
                                     | (title, content) VALUES
                                     | ('My Post', 'hello!')
                                     |
                                     | # ^^^^^^
                                     | # Boom! We now have a duplicate
                                     | # title!

Something to watch out for as you scale or build out your Rails app. This is the kind of stuff that only bites you when you get bigger.

The simple way to have your gem and eat it too: Symlinks to the rescue (Ruby on Rails Tip!)

Here's how to have your gem and eat it too. It's been quite a pain previously to try and a) modify functionality in a gem and b) also use it in a working dev project directory.

Previously, I've had to resort to git submodules or other horrendous integration points. But now (this is SO simple) I realize all I have to do is:
  1. Fork or check in your gem to github. Fully follow the instructions by github.
  2. Add config.gem 'username-gemname', :source => 'http://gems.github.com' to your environment.rb file.
  3. Now in your local rails project directory, under vendor/gems you can create a symlink from your local dev copy of your gem to username-gemname, e.g. garrytan-ebayapi-0.12.1

Tada! Now your production / staging / other development instances (and your dev buddies) will pick up whatever is checked in as a gem, but you'll be able to mess around with your local copy of the gem and work on it without any hassle.

Don't forget to update your VERSION, generate a new gemspec, and push to github to make sure everyone else picks up your changes when you're done making changes to your gem.

I've forked and added more functionality to the ebayapi gem

If you want to create multi-user eBay apps, you'll need the ebayapi trading API XML gem created by Cody Fauser to let you:

  1. Get a session id (the existing version does not conform to the correct GetSessionID, passing XMLRequesterCredentials node instead of the specified one that passes DevID, AppID and AuthCert.
  2. Pass an RuName for your app from the config

The existing gem doesn't support these, but I've added it today. I'll probably post again soon about how to set up multi-user tokens as mentioned here using the updated ebayapi gem.

Here's the updated ebayapi gem on github. To install it on your machine, you can run:

I've seen "IE/Opera not supported" -- but this is the first time I've seen "Firefox 3 not supported" on the eBay API site


Seen on developer.ebay.com.

Here's what I think:

XML-Mapping gem has really lame permission bug

You may have this error when require ‘xml/mapping’ in your ruby script:

/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require': no such file to load -- xml/mapping (LoadError)

Solution: go to the gem directory of XML-Mapping (mine is /usr/lib/ruby/gems/1.8/gems/xml-mapping-0.8.1/lib/) and chmod all files in this directory to 644.

Who is to point finger at here ? Rubygem or XML-Mapping authors ?

For some reason the default install has them installed with root-only permissions. I love how this is a Priority 3 bug assigned to nobody, and it blocks the use of this gem entirely.

Here's the fix (on OS X anyway)

sudo chmod -R a+r /Library/Ruby/Gems/1.8/gems/xml-mapping-0.8.1

Here's the thread to complain on.

What to do when your permissions get munged

find . -type d -exec chmod 755 {} \;

This will recursively search your directory tree (starting at dir ‘dot’) and chmod 755 all directories only.

Similarly, the following will chmod all files only (and ignore the directories):

find . -type f -exec chmod 644 {} \;

Handy.

Should you use a flash intro for your website? A detailed decision tree to help you.

Firefox 3.5 will let us have our beautiful web fonts. Death to sifr, finally, in 2009? Huzzah!

System administrators who force users to use IE6 should be ashamed of themselves. ASHAMED.

69% of IE6 users have no choice -- they have to use it. That's just sick. IT'S SICK AND PERVERTED. I want to vomit. I just did, a little, in my mouth.

Oops, running Windows Server costs London Stock Exchange a day in downtime. Time to switch to linux.

TradElect runs on HP ProLiant servers running, in turn, Windows Server 2003. The TradElect software itself is a custom blend of C# and .NET programs, which was created by Microsoft and Accenture, the global consulting firm. On the back-end, it relied on Microsoft SQL Server 2000. Its goal was to maintain sub-ten millisecond response times, real-time system speeds, for stock trades.

It never, ever came close to achieving these performance goals. Worse still, the LSE's competition, such as its main rival Chi-X with its MarketPrizm trading platform software, was able to deliver that level of performance and in general it was running rings about TradElect. Three guesses what MarketPrizm runs on and the first two don't count. The answer is Linux.