Adam @ Heroku
a tornado of razorblades

to_xml

March 16, 2008 at 07:57 PM

When serving up an ActiveRecord as resource, you can use options like :only and :except to to_xml. But in most cases, you want the same fields served every time - on create, update, and get. So put the options into the class itself:

def to_xml(options={})
  options[:only] ||= [ :name, :width, :height, :created_at ]
  super
end

ActiveRecord Setter Overloading

November 13, 2007 at 11:20 PM

When overloading an ActiveRecord setter, I've often done this:

   1  def name=(new_name)
   2    @old_name = name
   3    attributes['name'] = new_name
   4  end

However, this doesn't always seem to work (changes to attributes don't seem to stick). Recently I've started doing this:

   1  def name=(new_name)
   2    @old_name = name
   3    super
   4  end

Which seems cleaner anyway. I can't seem to find anyone talking about the correct convention - anyone know the right answer here?