Migrating db to ActiveRecord and rewriting logic