Problems with model.create

0 views
Skip to first unread message

Phillip Koebbe

unread,
Jul 8, 2008, 3:09:12 PM7/8/08
to Dr Nic's Magic Models
I'm trying to use MMC to create a system where I can migrate data from
one environment to another in a rake task. In my code, I'm attempting
to insert into the target table each row from the source table that
does not already exist in the target. An example is

def migrate_people(source_conn, target_conn)
source_model = "#{source_conn.name}::Person".constantize
target_model = "#{target_conn.name}::Person".constantize

source_data = source_model.find(:all)
source_data.each do |r|
conditions = ['email = ?'] + [r.email]
unless target_model.exists?(conditions)
target_model.create(r.attributes.reject { |k, v| k == 'id' } )
end
end
end # migrate_people

I pass to this method the modules that do the establish_connection
call. The finds work great, hitting the databases they are supposed
to. But the

target_model.create

does not. It hits the default environment's database. For example, if
source_conn points to the test_data environment and target_conn points
to the qa environment, the target_model.create statement actually
tries to create the record in the development environment. I have
verified this by running the code in script/console and watching the
database connections in pgsql.

I *think* this is happening because the magic class of
target_conn::Person doesn't have it's own create method, and so it's
getting passed up to the super class, which is just plain old Person,
which is connecting to the default environment. Unfortunately, I don't
have the brain power right now to verify this.

Can someone confirm or deny this? If this is, in fact, what's
happening, how can new records be inserted into these magic
connections?

Peace,
Phillip
Reply all
Reply to author
Forward
0 new messages