Sensorial’Org

Captious Lives!

It’s alive! Everything is still mostly the same, but we’ve brushed off some dust and breathed some air into it.

RoR: Setting up Postgres

One week in and still stumbling slowly along.

Setting up Postgres with Rails on Windows

  1. Install PostgreSQL. I used the One Click Installer. Version 8.3, at the moment. I left all the default settings, including the default user postgres.
  2. Install the Ruby Postgres driver so that it can talk to the database. From the Ruby console: gem install postgres-pr.
  3. Add Postgres \bin to the PATH environment variable so that Windows will know where to find the program when you’re trying to execute a Postgres command: Start > Settings > Control Panel > System > Advanced > Environment Variables > PATH > edit. Append the path to the Postgres \bin to the end of the value.

Talking to Postgres

  1. Specifying the database option (e.g. rails -d Postgres appName) when you’re creating a new Rails application will configure database.yml with the correct adapter. Otherwise, it’s just a matter of changing the adapter value from sqlite or mysql to postgresql. Similarly, migrating for an existing application will only require changing the settings in database.yml.
  2. Set up the databases. This can be done either through the command line of through the pgAdmin III, the GUI admin panel that comes with PostgresSQL.
    1. Using the command-line: Start > Programs > PostgreSQL 8.3 > SQL Shell
      1. Answer the prompts until you’re connected to the server.
      2. CREATE DATABASE db_name; adhere Rails convention to create and name the 3 databases. If successful, CREATE DATABASE will echo back at you.
    2. Using the GUI: Start > Programs > PostgreSQL 8.3 > pgAdmin III
      1. Double click on the PostgreSQL server under the Object Browser on the left, and you’ll be prompted for your password.
      2. Right click for New Database…: Stick to all the default settings.
  3. Migrate the application tables. Restart the web server, and on the Ruby Console: rake db:migrate.
  4. Verify that the migration was successful:
    1. Using the command-line SQL shell
      1. While you’re still logged in on the server, connect to your database: \c db_name.
      2. Do \dt to list the tables that should have been created during the migration.
    2. Using the GUI
      1. The tables should be under Servers > PostgreSQL 8.3 > Databases > db_name > Schemas > public > tables.

Helpful Links

  1. Converting and Verifying the Migration to PostgreSQL
  2. Switching Rails to Postgres (on Ubuntu)

RoR: First Application

Trying to learning Ruby on Rails is driving me absolutely insane for a number of reasons:

  1. I’m using windows, and most of my peers are either on Ubuntu, mac, linux/unix, or what have you. I could install a virtual machine, partition another drive for linux, buy a mac… but I just want to get my feet wet, not shave a yak.
  2. I’ve gone through 5 or 6 tutorials including the ever famous cookbook and blog examples, and none of them have worked out. Again because I’m using InstantRails on windows. That, or I’m using an outdated version or Rails, or the tutorial is outdated because the commands that they are using are no longer supported in Rails 2.x.
  3. One such example is the Scaffold generator which all the tutorials seems to be fond of using. Which is impressive… but useless to a newbie because the tutorial boils down to: Put this line here, refresh the page and voila! Everything is generated automatically. Okay… Rails is awesome, I’m convinced, but WTF did it just do? It’s too soon to be showing tricks, especially when the tricks don’t work and I end up getting errors I don’t understand!

But hope is not lost. Ruby on Rails 2.1.x is an up-to-date tutorial that will guide you through everything. It gets into ground-level details without all the frills that Rails has to offer, teaching you the basics including Rails conventions and paradigms. It even explains how you can use Scaffold, but at the end when you have a grasp about how you could write the code on your own.

In addition RubyLearning.com was extremely helpful in getting me caught up on the Ruby language.

Even with this, I did not get by without cursing a few times.

Issues

  • Error: “no such file to load — mysql”. The mysql driver was decoupled with Rails 2.2.*. You’ll have to reinstall it gem install mysql. Be sure to restart the web server: CTRL+C on the server console followed by ruby script/server.
  • It might be another “problem” with Rails 2.2.*, but when I generated models via ruby script/generate model modelName, a migration was automatically generated called yyyymmdd_modelName.rb. So when I generated a migration explicitly , I got an error telling me a table already exists. Login to mysql using your username and password, (mysql -u root in my case) and drop the tables and migration schema: drop table modelName; drop table schema_migrations. Follow-up by recreating the tables (this time, with all the required fields) using the migration scripts: rake db:migrate.
  • jEdit does not recognize *.html.erb as .*rhtml files, thus losing syntax highlight. Register the extension by going to Utilities > Global Options > Editing. Under “Change settings for mode”, select rhtml. Uncheck “Use default settings”. In the “File name glob” field, add *.html.erb” or alternative, change it to “*.{rhtml,html.erb}”. Apply.
    • A few hours later, I have my first app!


« Previous entries

Next entries »