/me@(voip.)?|www./ SamLown.com

Inheriting views in CouchRest ExtendedDocument

I recently decided to upgrade autofiscal to use the awesome CouchDB document based storage engine. The change from a traditional RDMS (ActiveRecord) to this new storage engine (CouchRest) has been pretty straight forward but there was still one tiny issue I hadn’t got round to resolving until now.

The scenario is of Sale and Purchase classes which inherit from the Invoice class. Methods and properties are all inherited correctly, but the views use internal logic based on the class name. As such, defining a view in the Invoice class will only perform lookups on documents whose couchrest-type is “Invoice”. To get around this issue we need to define the views at the moment the Invoice class is inherited:

class Invoice < CouchRest::ExtendedDocument
  def self.inherited(subclass)
    super
    subclass.class_eval do
      view_by :client
    end
  end
end

Using code like this, self.to_s when used inside the view_by will return the name of the class using the view.

written by Sam,


No comments yet


Write your comment



HTML will be removed, but Textile formating is permitted. Log in for your comment to be posted without review.