Read my latest article: 8 things I look for in a Ruby on Rails app (posted Thu, 06 Jul 2017 16:59:00 GMT)

New Active Record Options for Associations

Posted by Wed, 19 Oct 2005 22:00:00 GMT

7 comments Latest by dfhsdfh Mon, 21 Aug 2006 09:51:08 GMT

Two months ago today I posted about a bug in Active Record. A bug that reminded me to remain cautious about how much trust I put into a database abstraction layer. I am happy to now say that this particular bug has been fixed, and I got to help! In the process, I also got to add some new features. (see my original rant, Active Record, I love U but I still trust my database server a tiny bit more.

I discovered this bug when I was working on a chapter in my book on Active Record. I’m known to gladly take advantage of database constraints and triggers, and it was when I decided to test my code without these constraints, I discovered the bug. “Hey, Active Record isn’t doing what it’s supposed to!” I’ve since had a number of people ask me if what a more pragmatic way to work around this issue is, rather than go my route by adding a constraint/trigger.

You can now DO AWAY with ON DELETE CASCADE! (sort of)

The new release of Rails 1.0 Release Candidate includes some new options for the has_many and has_one declarations.

Previously, you could do the following:

class Customer < ActiveRecord::Base
  has_many :orders, :dependent => true
end

This was supposed to nullify the dependent records, but it didn’t!

My patch not only fixes this, but also gives more control with what :dependent does. Now, you can pass the :dependent option to the has_many and has_one declarations with either :nullify or :destroy. This has a similar affect as ON DELETE CASCADE in those fancy RDBMs like PostgreSQL.

Let’s take a closer look at these new options:

has_one

A spider has_one web, and the web belongs_to one spider. If you destroy the spider, you would most likely want to destroy the web as well.

class Spider < ActiveRecord::Base
  has_one :web, :dependent => :destroy
end

On the other hand, in the case of a snail that has_one shell (and the one shell belongs_to the snail), if you destroy the snail, you may want to keep the shell. Remember, your crazy Aunt Ruby collects snail shells.

class Snail < ActiveRecord::Base
  has_one :shell, :dependent => :nullify
end

Now, for every snail we destroy, the shells, though once dependent on the snail, are now available for Aunt Ruby.

has_many

The same rules apply to the has_many association. Most people assume that if you destory a beehive that you would destroy all the bees. I’d like to think that they wander around until they find a new beehive to join. So, we can :nullify their relationship when the beehive is destroyed, thus making them homeless, but available for future hives.

class Beehive < ActiveRecord::Base
  has_many :bees, :dependent => :nullify
end

Sadly, some people might want to destroy the bees along with the destruction of the beehive. So, those people can pass the :dependent option, :destroy.

class Beehive < ActiveRecord::Base
  has_many :bees, :dependent => :destroy
end

Don’t worry, your usage of :dependent => true will now work, even though it wasn’t working before.

I’m going to try to put more trust into Active Record now, and I hope that this new addition to the library finds itself useful for you. :-)

Take a peak at the cool new features in the latest version of Rails and see the Active Record CHANGELOG for more information.

Enjoy!

RubyGems mirror to kill the wait

Posted by Wed, 19 Oct 2005 03:38:00 GMT

Like Dan Peterson, I have setup a mirror of the RubyGems repository on the same server that is an official RubyForge mirror.

http://rubyforge.planetargon.com/gems.rubyforge.org


gem install --source http://rubyforge.planetargon.com/gems.rubyforge.org postgres

The RubyForge mirror team is working on a gem mirroring solution… and in the meantime, this is my attempt to help out with the load.

Dan also noted that you could modify the .gemrc file:

gem: --source http://rubyforge.planetargon.com/gems.rubyforge.org

Thanks Danp for pointing this out… and Enjoy!

Oops, Curt did it again

Posted by Fri, 14 Oct 2005 00:12:00 GMT

Let’s take a moment to say thanks to Curt Hibbs.

O’Reilly has just posted his fourth article of the year, What is Ruby on Rails.

To top it off, he announced the release Instant Rails yesterday!

CurtHibbs++

UPDATE It looks like Slashdot approved my post about the article that Curt wrote. :-)

Typo Theme Contest announced

Posted by Mon, 10 Oct 2005 16:33:00 GMT

Geoffrey Grosenbach has announced the Typo Theme Contest on TypoGarden.org, Yay!

PLANET ARGON has offered a year of Level 3 hosting for the winner. We also promised ten free blog hosting packages to the first ten people that submit new themes that meet the requirements of the content!

Good luck to all the players. :-)

(obviously… I could use a new theme too… I might have to participate!)

Small Ruby groups acting small

Posted by Thu, 06 Oct 2005 15:21:00 GMT

Portland Ruby Brigade

The Portland Ruby Brigade is kicking into full gear. We actually hit 20 people in our IRC channel this morning! Not bad for a group that has less than 40 people on the mailing list right now. We had ~30 people show up to the last PDX.rb meeting on October 4th.

Why are local user groups so important?

Rails has flooded the Ruby scene. We have people seeking more help in several mailing lists, IRC channels, and forums. This is where local user groups comes into play. Reading the manuals online, the books, and list archives are all great ways to learn something but often times it is the interaction with other human beings that really pushes our knowledge. Getting to meet up with local Ruby and Rails fans a few times a month, listen to discussions in the flesh, and having a beer with some really talented people is truly a great experience.

What can I do?

If your local area does not have a Ruby/Rails group… take the plunge and start something up now!

What if I live in Portland?

Just go there on the map on the first Tuesday of the month!

If you live anywhere near Portland and just a little bit interested in Ruby, you should swing by the PDX.rb IRC channel on Freenode or signup on the mailing list.

You can also read the PDX.rb blog here.

This is just the beginning… :-)

irb history hack

Posted by Wed, 05 Oct 2005 17:43:00 GMT

Okay, I’ve posted a few things about console recently and I know that you’re all using it… right?

Well, Jeremy Kemper (bitsweat) mentioned at the PDX.rb meeting last night that there was a way to keep history of your irb commands. (more than just readline… up-arrow’n).

You can actually quit irb, restart it and still see your history. This makes Robby happy.

See here for details

Thanks Jeremy!

Older posts: 1 2 3