Aug 20 2006

TextMate rules

dastels @ 4:28 pm

As part of my getting back on the rails train, I’ve decided to give TextMate another try as well. So far, so good. Im making a concerted effort to learn the ins & outs of it. I had been using RadRails, but it’s big, and rather slow at times. Also, it’s very obviously *NOT* a Cocoa application. It felt gratingly out of place.

TextMate truly is a nice editor.. it’s no big surprise it won an Apple design award at WWDC earlier this month.

Given my Emacs based “upbringing”, TextMate feels nice. I’m looking forward to getting to know this app well.

UPDATE: While TextMate is a sweet text editor, I’ve since decided that it is not the programming editor for me.. I’ve switched to GNU Emacs on OS X for that.


Aug 20 2006

has_and_belongs_to_many

dastels @ 2:19 am

For anyone learning rails in my wake, here the “Doh!” moments I had as I tried to get many-to-many relationships working. Either it took some digging to find the required information or it wasn’t immediately obvious.

Here’s my list of things I found by trial/error/reading/ and lots of googling (and, yes, it is a verb now.. I’ve been using it as a verb for at least 3 years).

  1. Put has_and_belongs_to_many in each class, naming the other class (it’s table name really… a plural)
  2. Add instances of one class to the collection in the other, e.g. order.items << album. Do this in only one direction.
  3. Before adding to an object’s collection, save the object. E.g
    if order.save
      order.items << an_item
    

    It seems that this is only required in the case where order is a freshly created object. This makes sense as it will not have an id (for the join table) until it is saved. Furthermore, both objects will have to have been previously saved (i.e. have ids).

  4. The join table is made up of the names of the two related tables.. in alphabetic order. E.g. items_orders, not orders_items
  5. Don’t put an id column in the join tables. Mine had such a column because I sketched out my schema in a migration, not realizing this.
  6. In your migrations, use :id => false as a second argument to create_table, e.g.:
    create_table :items_orders, :id => false do |t|
      t.column :item_id, :integer
      t.column :o rder_id, :integer
    end
    

If I’ve missed anything on this list, please let me know and I’ll add it.


Aug 20 2006

Delving ActiveRecords secrets

dastels @ 1:50 am

My ramp up on Rails continues. Today I’ve been getting familiar with ActiveRecords table relationship facilities. Pretty nice. One-to-one and one-to-many were trivial… although I’m currently stuck on getting many-to-many relationship working. I keep making progress in little steps, though.




Steve Jobs Memorial

Stay Hungry, Stay Foolish!
Steve Jobs

In memory of Steve Jobs