Archive for February, 2009

Lots of love going about today! This time, the recipient is SSHFS which allows you to mount a remote filing system onto your local tree over SSH. In a virtual hosting environment (/vhosts/ etc) it’s pure joy to work with.
You’ll need to install SSHFS on the client (the machine you want to mount the remote [...]

Today I had a log table report query that contained a nested query. It was taking ages (5mins) to complete. I investigated indexes which I had been meaning to look at for a while. Anyway I managed to bring down the query to 0.04s – I now have much love for indexes!!
Basic MySQL syntax:
1CREATE INDEX [...]

Handy, brings you back a time difference in hh:mm:ss format inside an MySQL query:
1TIMEDIFF(date_time_one, date_time_two)

One for my own records….
It’s:
1cap deploy:migrations
Not!
1cap deploy:migration

Add this to your ~/.bash_profile (~/.profile on OSX) and gain a ‘trs’ shortcut to reboot mod_rails. Bliss ;]
1alias trs=’touch tmp/restart.txt’

When rendering a collection of partials in Rails you can access a sneaky iteration counter from inside each partial using the partial name appended with _counter:
So in the view / partial that calls the collection of partials:
1<%= render :partial => "partial_name", :collection => stuff_to_loop_over %>
Then in your called partial:
12<%= partial_name_counter %>
# Outputs the count 0,1,2,3,4 [...]

To dump an SQL file of just the schema (structure) of a MySQL database table you can use this command line:
1mysqldump -du username -p databasename tablename > filename.sql
You can drop the -p if you don’t have a password.
Useful if you need to move table structures around ;]

To return a class name as a string in Ruby you need this code:
Ruby:
1class_name = thing.class.to_s
I’m also reliably informed you can do this too. Not tested it yet though:
1class_name = thing.class.name
That feels more logical to me.
PHP equivalent:
1$class_name = get_class($thing);

I managed to instantiate Rails Active Record models dynamically with the following code:
123456table_name = ‘table_name_string’
model = Class.new(ActiveRecord::Base) do
   set_table_name table_name
end

model.find(:all)
This would roughly be the equivalent in PHP:
12$object_name = ‘object_name_string’;
$object = new $object_name;

Following on from my last post. I needed to set an object attribute/member dynamically in Ruby:
In PHP that would be:
12$member_variable = ‘member_variable_name’;
$object->$member_variable = ‘value’;
After the usual stack of Googling, the Ruby way:
12member_variable = ‘member_variable_name’
object.send((member_variable + "=").to_sym, ‘value’)


top