in reply to Object Persistence, Moose and playing nicely with ActiveRecord

Suppose that I don't know anything about Ruby, Ruby On Rails or ActiveRecord except for the horror stories that roam the interwebs. How would you explain "single table inheritance", "composed_of" and "various other bells and whistles" in terms that make sense to the outside world?

Basically, if you can tell us what the SQL statements are and possibly even how the object API is, maybe we can find equivalent things in the Perl world.

Possibly, DBIx::Class does lots of what you want, but then again, I've become a friend of raw SQL for anything less trivial than updating a single column or row, because ORM wrappers don't work for me.

  • Comment on Re: Object Persistence, Moose and playing nicely with ActiveRecord

Replies are listed 'Best First'.
Re^2: Object Persistence, Moose and playing nicely with ActiveRecord
by pdcawley (Hermit) on Sep 08, 2008 at 14:59 UTC
    Single table inheritance. You have a class, Document, stored in the documents (I know) table you have:
    package Review use Moose extends 'Document'
    The documents table has a set of columns which is the superset of attributes of Document and all its subclass and a type column which contains the name of the class for that row. Document->find finds Documents, Reviews and any other subclasses of document, Review->find finds only reviews (and any subclasses).

    Composed of: A way of getting round primitive obsession. You can declare something like:

    composed_of :price, :class_name => 'Money', :mapping => %w{cents currency}
    And ActiveRecord handles turning cents and currency columns into a Money object when the object is pulled out of the database breaking the price into cents and currency the the object's put back in there.

    Bells and whistles: Mostly to do with declaring relationships between classes with has_many/belongs_to etc. If you want me to regurgitate the ActiveRecord::Base documentation then I will, but it's far from being the world's best documentation in the first place and my paraphrasing skills are poor. Most of the perly ORMs appear to have sensible equivalents already so it won't be beyond my wit to sort out the mapping.

      There are inheritance modules for Class::DBI and Tangram, but I haven't seen any for Rose::DB::Object or DBIx::Class. It's a relatively easy thing to build though, just making classes for each type. The only tricky part is turning Documents into Reports, which I've implemented in my own Class::DBI stuff before by having the base class look at a type column and then instantiate an object of the right class with the current primary key.

      The rest of the stuff is standard issue ORM functionality that all the Perl ones have.

      I'm not aware of any Perl-ORM implementing Single Table Inheritance (or rather, Single Table Object Storage). I guess you can fake this by creating updateable views for all classes if your underlying DB supports that.

      For composition, I think DBIx::Class (like Class::DBI) has inflation/deflation links which can be used to instantiate the appropriate objects for fields. But I haven't used either.