On a recent project I came across a problem that I hadn’t given much thought before. In Rails association of one class to another is pretty straight forward. For example, if you had a Dog class and a Dog instance had one instance of the Treat class, you can simply make this association via has_one :treat in your Dog class. This works great if you have complete control over the Treat class and can assign a dog_id to the treat database table. But what if the Treat class is outside of your root application, perhaps an object created by a third party gem that you want associated with your Dog class. This is where serialization comes in very handy. If we continue with our Dog class example, below we see a pretty basic Rails class.

1
2
3
class Dog < ActiveRecord::Base
  serialize :treat
end

Now we just need add the treats column to our dog table.

1
2
$ rails generate AddTreatToDog treats:text
$ rake db:migrate

It’s important to assign the treats column as a text data type to insure #serialize works properly.

1
2
fido = Dog.new
fido.treat = OutsideGem::Treat.new

That’s it! Now we have access to all the methods available to the Treat class.

1
fido.treat.healthiest #=> "Primal Pet Foods"