`
jiajie0531
  • 浏览: 27556 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Active Record Basics 4 Overriding the Naming Conventions

阅读更多

version: rails4

4 重写命名约定

 

如果你需要遵循一个不同的命名约定,或者需要在你的Rails应用系统中使用一个遗留的数据将会怎么样?没有问题,你能够容易地重写默认的约定。

 

你能够使用 ActiveRecord::Base.table_name=方法来指明数据表名,类似于下文:

 

classProduct < ActiveRecord::Base

  self.table_name = "PRODUCT"

end

如果你这样子写了,你就能够去手动地定义类名,在你的测试定义中用set_fixture_class方法来集合fixtures(class_name.yml):

classFunnyJoke < ActiveSupport::TestCase

  set_fixture_class funny_jokes: Joke

  fixtures :funny_jokes

  ...

end

同样能够重写列,应该被使用的如同数据表的主键,使用ActiveRecord::Base.primary_key=方法:

classProduct < ActiveRecord::Base

  self.primary_key = "product_id"

end

 

 

original: http://guides.rubyonrails.org/active_record_basics.html#overriding-the-naming-conventions

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics