#25 ✓resolved
Sam Smoot

Column copy for STI should move to Table::new

Reported by Sam Smoot | October 12th, 2007 @ 08:00 AM

So here's the problem: Two different Database instances. You load the model in-scope for both such as this snippet from http://datamapper.rubyforge.org/... (take note of the "LOADING MODELS" comments):

def load_models
  Dir[File.dirname(__FILE__) + '/models/*.rb'].sort.each do |path|
    load path

DataMapper::Database.setup(:default, configuration_options)

database do |db|
  load_models # LOADING MODELS

mock_db = DataMapper::Database.setup(:mock, {})
mock_db.adapter = MockAdapter.new(mock_db)
database(:mock) { load_models } # LOADING MODELS

The problem is that a STI model like SalesPerson < Person uses the inherited hook to copy the properties from the superclass. The inherited hook only fires the first time though, so the properties aren't copied the second time around. So the first database in this example has the proper setup, the second will only map the columns explicitly defined by the subclass.

You might think: "Why don't we just move the mappings into the class?". The answer is that we won't be able to have database-specific mappings like column overrides then. Plus that leans more towards the AR philosophy where one uber-class does everything and I just don't like that. ;-)

So this ticket should not involve massive refactoring. The general classes involved need to more or less stay as they are. We just need to fix the method for inheritance to accomodate multiple databases. Something probably not too dissimilar to this:

class Table
  def initialize(adapter, klass)
    if klass.ancestors.include?(DataMapper::Base) && klass.superclass != DataMapper::Base
      adapter.table(klass.superclass).columns.each do |column|
        self.add_column(column.name, column.type, column.options)

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket


Referenced by