将rails连接到控制台和控制器看不到的现有postgres DB模型



我有一个包含多个表的数据库,每个表都包含可能不遵循rails命名约定的列
有没有一个工具可以从这些表中创建ActiveRecord模型,或者我需要一个接一个地在手边做这件事?

如果我手工为一个表创建ActiveRecord模型,这可以吗?(上面不需要隐藏的数据库标识符?)

更新

我尝试过magicmodels,但无法使其工作(自从上次修改以来已经有一段时间了),并且似乎与rails 3.2 不兼容

我当时尝试了什么:

- change the database.yml so it points towards my existing Postresql database
- manually create my models such as:
    # app/models/user.rb
    class User < ActiveRecord::Base
    end
- run the console and tried
    User.all
=> I end up with an error saying that contant User was not initialized.  
Doesn't the console import the model automatically ? Or is that linked to the fact the configuration I did is not correct ?

http://magicmodels.rubyforge.org/magic_model_generator/可能就是你想要的。不过,我还没有听说过很多提供这种功能的工具,因为许多rails应用程序都是从头开始设计的,而不是给它一个遗留数据库,然后用它创建模型。

您可以很容易地手工创建模型,并将它们映射到几乎任何数据库表中。模型有一个"set_table_name'name'",它允许您重写单个模型映射到多个数据库表名称的rails默认约定。

ActiveRecord可以正常使用遗留数据库。我做了一个后端系统,它不使用带有ActiveRecord的Rails作为我的ORM。"ActiveRecord Without Rails"让我开始了。"在Rails之外使用ActiveRecord"也很有用。在谷歌上搜索"使用没有轨道的活动记录",你会发现更多。

你不需要一个丰满的模型。只需为所需的表使用基类,ActiveRecord就会查询数据库所需的内容。它不会知道表关系,但对于一般查询,它会做得很好。在你需要的时候建立关系。

最新更新