每天一剂Rails良药之Integrating with Legacy Databases

针对遗留系统的二次开发或者需要使用遗留数据库时,我们可以突破默认的Rails数据库表名惯例,秘笈就是配置ActiveRecord::Base

为了在整个Rails应用域里起作用,我们将配置写在config/environment.rb里:
ActiveRecord::Base.table_name_prefix = "wp_"

ActiveRecord::Base.pluralize_table_names = false

ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore

ActiveRecord::Base.set_primary_key = "identifier"

当然也可以在某一个model类里定义表名和主键名:
class Order < ActiveRecord::Base
  set_table_name "wp_order"
  set_primary_key "orderId"
end

你可能感兴趣的:(Rails,ActiveRecord)