HABTM before_filter in rails models

I’ve been playing about with Devise and CanCan for rails authentication and authorization recently - this great pair of posts from Tony Amoyal have helped a great deal. I have a User model and a Role model, and wanted to automatically add the :user role to each user whenever roles are assigned. The roles were set in the controller as follows: ...

October 3, 2010 · 2 min · Dave Perrett

Compiling mysql-python on OS X leopard

I’ve just finished trying to install mysql-python on OSX Leopard, and had quite a few problems. This is the procedure that finally ended up working for me. First, download mysql-python from here. Unzip and try to build it : ...

August 18, 2009 · 2 min · Dave Perrett

'Hello World' with Scala and Lift on OSX

Obviously we’re going to need scala before we can do anything useful; the easiest way to install it is via macports . We’re also going to need maven. > sudo port install scala > sudo port install maven2 Scala seems to have moved their repository to github, and the old google code repository no longer works. Assuming you have git installed (if not, there’s an OS X installer here) : ...

August 17, 2009 · 2 min · Dave Perrett

mysql select random row with join

I have a users table and an assets (basically just images) table, and I want to select and display a random asset for each user. I want to show a list of users, with a random thumbnail for each user from their assets. I could do this easily enough in code with multiple queries, but it seemed like a nice challenge to try and find a pure-sql alternative. ...

August 11, 2009 · 2 min · Dave Perrett

InnoDB as default MySQL table type

To change the default MySQL table type, edit your my.cnf file (usually /etc/mysql/my.cnf on ubuntu), and add the following line to the [mysqld] section : default-table-type=innodb The resulting config will look something like this : ...

August 9, 2009 · 1 min · Dave Perrett

MySQL table 'is marked as crashed'

I recently had a problem trying to use mysqldump where it was complaining that mysqldump: Got error: 145: Table './recurser/wp_options' is marked as crashed and should be repaired when using LOCK TABLES This corrupted wp_options table actually caused wordpress to think it was a fresh install, and showed the interface to create the admin user to anyone who accessed recurser which is a little scary :) The solution was to go to the actual database directory, and run myisamchk -r on the offending table : ...

June 18, 2009 · 1 min · Dave Perrett

Exclude tables from mysqldump

Recently I was in a position where i wanted to exclude 2 large tables from mysqldump, without specifying every single table I wanted to back up. After a bit of searching, it seems you can easily exclude tables from mysqldump using the –ignore-table option : > mysqldump -u dave -ppassword -h localhost --ignore-table=my_db_name.my_table_name my_db_name …obviously substituting your own username, password, db name and table name etc . As far as i can tell you can use this option multiple times to exclude multiple tables

June 2, 2009 · 1 min · Dave Perrett

undefined method assert_valid_keys in Rails

If you are getting an error about ‘undefined method assert_valid_keys’ in Rails : undefined method `assert_valid_keys' for :time_entry_product:Symbol … it may be because you have two relationships specified on the same line in your model : ...

February 24, 2009 · 1 min · Dave Perrett

Installing Trac on CentOS

Before you do anything, go ahead and install subversion . Install the python easy install tool: ...

July 27, 2008 · 3 min · Dave Perrett

Force MySQL Encoding to UTF8

Often MySQL will need to be forced to return results in UTF8 encoding. There are two ways to do this: ...

June 23, 2008 · 1 min · Dave Perrett