Better in-memory associations with :inverse_of

Today Rails gained a very interesting commit from h-lame. It’s the first major step towards making associations aware of their parent model while still in-memory. A few days ago I started this discussion which revealed to me that it had been a long time coming, just no one bothered.

Well, finally someone did. I would like to thank h-lame for making this commit happen. Here’s the excerpt from the commit message.

You can now add an :inverse_of option to has_one, has_many and belongs_to associations. This is best described with an example:

      class Man < ActiveRecord::Base
        has_one :face, :inverse_of => :man
      end

      class Face < ActiveRecord::Base
        belongs_to :man, :inverse_of => :face
      end

      m = Man.first
      f = m.face
Without :inverse_of m and f.man would be different instances of the same object (f.man being pulled from the database again).

Installing Sphinx with Postgres on Leopard

I didn’t find a quick short guide to installing sphinx on Leopard with PostgreSQL, hence here goes (this assumes you already installed PostgreSQL).

Compile dependencies

These two libs are required by sphinx according to this blog post.

  ~% mkdir src
  ~% cd src
  ~% curl -O http://ftp.gnu.org/gnu/libiconv/libiconv-1.13.tar.gz
  ~% tar xzf libiconv-1.13.tar.gz
  ~% cd libiconv-1.13
  ~% ./configure --prefix=/usr/local
  ~% make
  ~% sudo make install
  ~% cd ..

  ~% curl -O http://internap.dl.sourceforge.net/sourceforge/expat/expat-2.0.1.tar.gz
  ~% tar xzf expat-2.0.1.tar.gz
  ~% cd expat-2.0.1
  ~% ./configure --prefix=/usr/local
  ~% make
  ~% sudo make install
  ~% cd ..

Compile Sphinx

At this point latest stable release was 0.9.8.1.

  ~% curl -O http://www.sphinxsearch.com/downloads/sphinx-0.9.8.1.tar.gz
  ~% tar xzf sphinx-0.9.8.1.tar.gz
  ~% cd sphinx-0.9.8.1
  ~% export LDFLAGS="-L/usr/lib"
  ~% ./configure --prefix=/usr/local --with-pgsql --without-mysql
  ~% make
  ~% sudo make install

You’re all set.

References

Shmacros

I use shoulda constantly as it is my favorite testing framework. I figured the custom macros I write may prove useful to others, thus I released them as a plugin called shmacros. As of now it includes the following methods.

<p>It&#8217;s best to have <a href="http://mocha.rubyforge.org">Mocha</a> installed as some of the macros rely on its presence.</p>
  ~/dev/myapp% sudo gem install mocha
  ~/dev/myapp% script/plugin install git://github.com/maxim/shmacros.git