Notes on Learning Rails part2

How to change your database tables?  Use a migration – it’s easy!

While building you first Rails app, you’re more than likely to change the design of your database tables a few times.  For example I suddenly decided that I wanted some columns on of my tables to be foreign keys to another database.  In any other language this would be as simple as writing a bit of SQL to change the column names.  Actually if it wasn’t for Rails needing to know when you use foreign keys, you wouldn’t even have to change the names, but Rails needs them to have _id at the end of the table column name just so it can pick up on your database structure.

Anyway… this is how you do it.

First get out of the rails console if you are in it, we don’t need to be in there for this.

1. in your project directory run the command:

rails generate migration change_my_column_name_migration

The more descriptive you can be with your migration name, the better.  I generally opt for longer, more descriptive names for things that I am only going to use once or twice.  This is one of those cases, you’ll only have to type it out once more.

2.  open up the file that it created: db/migrate/2423(timestamp)0000_migration_name.rb

3. in the self.up method, put this:

rename_column :table_name, :the_old_column_name, :the_new_column_name

For my case i was just adding “_id” to the end of my column name, so here’s an example of what I used

rename_column :trades, :trade_currency, :trade_currency_id
rename_column :trades, :base_currency, :base_cu
rename_column :trades, :trade_currency, :trade_currency_id

4.  You’ll want to make a reciprocal statement in the self.down method so rails can rollback if it needs to.

Here’s what my entire migration file looked like:

class ChangeCurrencyColumnName < ActiveRecord::Migration
def self.up
rename_column :trades, :trade_currency, :trade_currency_id
end
def self.down
rename_column :trades, :trade_currency_id, :trade_currency
end
end

5. Save your file and go back to the command prompt.  Enter the following command:

rake db:migrate

6. Your done!

I find that this solution is easier than trying to rollback, edit an existing migration file, then rerun the existing migration.  Editing existing migrations may erase your embarrassing mistakes in the past, but having loads of migration files sitting around is pretty common for any large rails app.  So you might as well get used it.

Notes on learning Rails

Everything I wish a book or tutorial had just come out and told me:

How to install a gem.

Most of the time people will tell you to add the gem to your gemfile and bundle it.  But what does that actually mean for the total noobs?!?

1. Open your file browser and go to your project directory.

2. Open the file called “Gemfile”  there is no file extension.  You don’t want “Gemfile.lock”.

3. add your gem with the same syntax your other gems are using there:

ex.  gem ‘rails’, ‘3.0.5′

includes the version number or

gem ‘haml’

to not include the version number

just so you know, all the lines that start with # are commented out, so unless you have a lot of gems, there may not be much in there

4. open the terminal in your project directory and run this command:

sudo bundle install

5. Your done!  That was super easy!

Neural-Networking library for Python

I’ve been having trouble finding a good library or module for neural networking in Python. I am working on a project this semester: Facial Expression Recognition with the Future Goal of Detecting Micro-expressions.
If anyone has any advice, I would greatly appreciate it.

WordPress at Home and at Work

I hope that this blog will be useful to the public someday.  But even if it isn’t, I’m glad to gain the experience in using WordPress.  I really impressed my boss the other day when I used WordPress to create a website for our research lab.  The software tools that they have been developing are ready for the public and they wanted me to create a simple website for publishing their work online.  I don’t think that they expected me to finish the site in half a day!  If you’d like to see it, it’s online here.  It was fun to use such a newly learned skill at work.  I decided to also take an online PHP class, just so that I can understand what goes in to making software like WordPress so powerful, yet simple.

Documenting with Sphinx

At first I was apprehensive when my boss asked me to start porting all of the lab’s software documentation to pdf and html using Sphinx (http://sphinx.pocoo.org/).  It took a couple days to find a version that would install both on our local Widows machine as well as the Fedora server.  I must say that following the rubric provided by Sphinx, the conversion was pretty straightforward.

Now that the majority of the information has been converted into reST (Restructured Text), I am looking forward to messing with the CSS for a customized look before publishing to the web.

Your comments or questions are much appreciated below!

SAGE – the ultimate Python development kit.

So I was checking out SAGE (http://www.sagemath.org/) and it seems very cool. This guy certainly had a lot to say about why switching from MATLAB to SAGE was extremely beneficial.  I agree with him for the most part.  I would love to see all the software we are building to be converted into Python.  Currently everything has been written in MatLab.  Also now that we are ready to start publishing our work onto the web and collaborate with other genomic imaging labs throughout the country, we are trying to find the best way to do that.  Python is free.  The new GUI is going to be in Python.  Everything else is in MatLab.  This is going to be a lot of work.  I’ve seen a couple different Python Libraries that will run MatLab syntax, but I believe our programs are a little too complex to run that way.  We’ll see what the boss decides to do.

Griffith Research

My research in the Image Processing Lab at Griffith University is going very well.  I just recently completed a program that identifies blood vessels in retina scans.  Over the next few weeks I will be testing the program in hopes to attain a high level of accuracy for vessel detection.  I used the Python Image Library (http://bit.ly/1ikFGj) and NumPy (http://bit.ly/2P9kn).  Using the Python programming language for biomedical research was something new for me because I before I had only used Python for game development.

Using the ideas of Elisa Ricci and Renzo Perfetti described in “Retinal Blood Vessel Segmentation Using Line Operators and Support Vector Classification” published in October, 2007 by IEEE Transactions on Medical Imaging, I believe my results are very close to the results they achieved.

Car Friction Simulation in Python

While playing around with a car driving simulation I was creating for my game programming class, I devised a class that simulates the lateral frictional forces that act on a car at high speeds.  Using the down arrow as a normal brake and the spacebar as a hand brake that “locks the wheels,” it is very easy to be thrown into a spin or skid out of control.
To do this, vectors must be used. The combination of velocity, momentum, acceleration, and friction all play a part on the direction the car is travelling.  More importantly, the car does not always travel in the direction it is facing, so the sprite image of the car had to be drawn independant of the car’s velocity vector.

Subwoofer Placement Simulation with Maple

While designing the home theater in our basement, David and I were arguing on the placement of the subwoofer in the room.  So I wrote a math program using Maple that uses a graphical map to display the reaction of individual frequencies against the concrete walls in our basement. Given a location in the room, the program will show where to expect constructive or destructive interference based on single reflections off the walls in the room.

After running the program, blue, green, red, and grey lines indicate areas of interference for different wavelengths off each wall. The best listening position is then determined to be the area in the room that has the fewest lines going though it. Of course, it is also used to find where in the room to place the subwoofer so that there are the fewest lines going through the middle of the room where people would naturally want to sit