轨道3 - 帕斯卡案列名工作轻松 [英] Rails 3 - Easily work with Pascal Case column names

查看:105
本文介绍了轨道3 - 帕斯卡案列名工作轻松的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开始一个Rails 3前端一个用Java编写的W / SQL Server数据层新开发的应用程序。

We are beginning a Rails 3 front end to a newly developed application written in Java w/ SQL Server data layer.

由于球队的previous的经验,所有的列名在Pascal大小写(ID,姓名,MyTableId)和表名在单数形式(而不是复数)。有没有一种方法可以轻松地在全球范围覆盖默认轨公约,这个数据模型的工作?

Due to the team's previous experience, all column names are in Pascal Case (Id, Name, MyTableId) and table names are in singular form (not pluralized). Is there a way to easily globally overwrite the default rails conventions to work with this data model?

我意识到这可以通过使用set_table_name和列别名来完成的,但那种失败的使用Rails快速开发应用程序,因为我们会重复我们现有的领域层codeBase的目的。

I realize this can be done by using set_table_name and column aliasing, but that kind of defeats the purpose of using Rails to quickly develop the application as we would have to duplicate our existing domain layer codebase.

我发现下面的code段将在一定程度满足我的需求,这是最好的方法/这是仍然有效?

I've found the code snippet below that will somewhat satisfy my needs, is this the best way / is this still valid?

http://snippets.dzone.com/posts/show/2034

推荐答案

你的问题的一部分是由ActiveRecord的:: Base的一个选项得到解决。在您的environments.rb,把

One part of your problem is solved by an option on ActiveRecord::Base. In your environments.rb, put

ActiveRecord::Base.pluralize_table_names = false

您其他的问题,据我所知道的,必须由跳水解决入模型生成code和发现的地方按摩你的属性纳入其列名。一旦数据库及其架构按照你的习惯,ActiveRecord的应(从我的理解),自动生成与该公约的属性。最简单的方法来测试,这是创建与您约定一个快速的记录,创造这个记录的一个模型,然后用导轨/控制台打印在该模型的实例column_names的结果。

Your other problems, as far as I can tell, will have to be solved by diving into the model generator code and finding where it massages your attributes into its column names. Once the database and its schema follow your convention, ActiveRecord should (from my understanding) automatically generate its attributes with that convention. The easy way to test this is to create a quick record with your conventions, create a model on that record, and then use rails/console to print the result of column_names on an instance of that model.

编辑:从上面你的编辑,我可以看到的ActiveRecord的method_missing可能强制执行的情况下。使用该链接要解决这个问题,虽然。

From your edit above, I can see that ActiveRecord's method_missing probably enforces case. Using that link should solve that issue, though.

千万要小心,当然,前提是使用帕斯卡情况下可能会导致有些头疼了Ruby,因为资本项目没有范围解析操作符取为常量,而不是对象。例如:

Do beware, of course, that using Pascal case may cause some headaches with Ruby, since capitalized items without a scope resolution operator are taken to be Constants, not Objects. For example:

class Foo
  attr_reader :low,:Cap


  def initialize
     @low = "lowtest"
     @Cap = "Captest"
  end

  def bar
     self.low   #<==works
     self.Cap   #<==works
  end

  def baz
     low        #<==works
     Cap        #<==raises NameError
  end
end

这只是意味着你的模型code将需要正确地使用,你应该反正做的。

This will just mean your model code will need to properly use self, which you should be doing anyway.

这篇关于轨道3 - 帕斯卡案列名工作轻松的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆