Main image
4th April
2010
written by simplelight

Assuming you want to make sure that no two models have the same COMBINATION of values for a and b:

so having two models with:

a = 1, b = 2
a = 1, b = 3

would not be a conflict.

If that’s the case then the standard validation

class Widget < ActiveRecord::Base
validates_uniqueness_of :a, :b
end

wouldn’t work since it tries to prevent saving two models with the same value of a, OR with the same value of b.

And even if that’s not what you’re trying to do, and you’re ok with the example being a conflict, validates_uniqueness_of doesn’t guarantee uniqueness if two users try to save conflicting records simultaneously.  The validation works by first trying to find a record with the value, and if it doesn’t find it inserting the ‘new’ record, and this can fail due to a concurrency hole.

To fill this hole requires leaning on the database server, and the way to do that in SQL is by having a unique index on the table which covers the column or columns you want to be unique. This assume you are using a database which supports it, e.g. MySql.

To create an index you can create a migration which includes a statement like

add_index  :widgets, [:a, :b], :unique => true)

Assuming that the table name for the model is ‘widgets’

Now if you do this, you also need to be aware that if you try to save a record with a uniqueness conflict the save will raise an ActiveRecord::StatementInvalid exception, which you’ll need to rescue and do something like telling the user of the conflict so that he can rectify it.

4th April
2010
written by simplelight

This is a great post on using the various jQuery Traversing functions to select DOM elements.

2nd December
2009
written by simplelight

>> ActiveRecord::Base.connection.instance_variable_set :@logger, Logger.new(STDOUT)

6th November
2009
written by simplelight

After downloading the latest Ubuntu desktop appliance from VMware, the following steps are required to get a Rails environment up and running:

Create a new user and add all the required access

Get Ubuntu up to date

sudo apt-get update
sudo apt-get dist-upgrade

Install compile packages

sudo apt-get install build-essential

Install Ruby, MySQL and the Open SSL library

sudo apt-get install ruby ri rdoc mysql-server libmysqlclient15-dev libopenssl-ruby ruby1.8-dev

Install Sun Java

sudo apt-get install sun-java6-jre sun-java6-plugin

Get the latest ruby gems from RubyForge

wget http://rubyforge.org/frs/download.php/57643/rubygems-1.3.5.tgz
tar xvzf rubygems-1.3.5.tgz
cd rubygems-1.3.5
sudo ruby setup.rb

Remove the .tgz file and erase the rubygems-1.3.5 directory too.

Create the following symbolic links

sudo ln -s /usr/bin/gem1.8 /usr/local/bin/gem
sudo ln -s /usr/bin/ruby1.8 /usr/local/bin/ruby
sudo ln -s /usr/bin/rdoc1.8 /usr/local/bin/rdoc
sudo ln -s /usr/bin/ri1.8 /usr/local/bin/ri
sudo ln -s /usr/bin/irb1.8 /usr/local/bin/irb

Install Rails

sudo gem install rails

Add Github for legacy gems

gem sources -a http://gems.github.com

Switch to Gemcutter for primary gem repository

sudo gem install gemcutter
gem tumble
14th September
2009
written by simplelight

The complete guide to handling web sessions in a Ruby on Rails framework

18th August
2009
written by simplelight

One of the promises of the internet has always been the collapsing of the pipeline between content creators and content consumers. We have already witnessed this phenomonen in the newspaper industry as the cost of distributing news fell from over $100 per subscriber per year to fractions of a penny.

As internet technology improves the same will happen to movies and television. Vuze, formerly Azureus, is a Silicon Valley startup that is at the forefront of this trend. By utilitizing peer-to-peer Bittorrent technology, Vuze has inverted the usual relationship in video streaming between scale and performance. Most internet streaming video degrades less than gracefully as more users watch a given stream. With peer-to-peer technology, the more people who watch the same show as you, the better your quality will be. Not only that, as more viewers join the network, the cost of delivering a high definition video stream to your TV, iPod or laptop declines to zero. With millions of concurrent users at any one time on the Vuze HD network (as of July 2009), you can be sure that someone will be watching what you are.

Just as the newspaper empires took over a decade to crumble, it’s likely that the large production studios will defend their fortresses for as long as possible. But in the long run, creative producers and quality content will gravitate to the cheapest distribution network. Consumers will pay less for their television shows, and the people who create the shows we watch will keep more of the profit.

17th August
2009
written by simplelight

I have been meaning to write an insightful post on the healthcare crisis but have been swept up in a more significant event: the invasion of a family portrait by a squirrel at Banff.

Squirrel Invades Photo

Don’t miss the inevitable post-event coverage

16th August
2009
written by simplelight

If you’ve always found using floats in CSS to be mostly trial and error then this Floatorial might clear up matters a little.

13th August
2009
written by simplelight

Website for international flower delivery: Flora 2000

Previous
Next