接轨现有的Postgres数据库 - 不会看到控制台和控制器型号 [英] Connect rails to existing postgres DB - models not seen by console and controllers

查看:358
本文介绍了接轨现有的Postgres数据库 - 不会看到控制台和控制器型号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个表的数据库,每一个都包含列可能不遵循导轨命名约定。
是否有现有的创建从这些表的ActiveRecord模型或者我需要手头做到这一点,一个接一个的工具?

如果我用手工打造的ActiveRecord模型为一个表,这将是确定关系吗? (没有隐藏DB标识需要在它的上面?)

更新

我已经试过magicmodels,但不能有工作(这已经有一段时间,因为这是最后一次修改),并似乎并不与导轨兼容3.2

我试过那么:

   - 更改的database.yml,使其指向对我现有的Postresql数据库
 - 手动创建我的模型,如:

    #应用程序/模型/ user.rb
    类用户的LT;的ActiveRecord :: Base的
    结束

 - 运行控制台并试图
    User.all

=>我结束了一个错误,指出contant用户未初始化。
不控制台自动导入模式?或者是说与以下事实我做了配置不正确?
 

解决方案

ActiveRecord的工作原理与传统的数据库确定。我这样做,并没有使用Rails的ActiveRecord的用我的ORM后端系统。 的ActiveRecord没有Rails的让我开始。 使用的ActiveRecord外导轨也是很有用的。谷歌搜索ActiveRecord使用无轨,你会发现更多。

您不需要一个完全充实模式。只需使用一个基类所需的表和ActiveRecord的将在数据库中查询它需要什么。它不会知道表关系,但是对于一般的查询,它会做的很好。构建关系,因为你去,需要他们。

I have a database with several tables, each one containing columns that may not follow the rails naming convention.
Is there a tool existing to create the ActiveRecord models from those tables or do I need to do this at hand, one by one ?

If I create the ActiveRecord model for one table by hand, would this be ok though ? (no hidden DB identifier needed on top of it ?)

UPDATE

I have tried magicmodels but cannot have it working (it has been a while since it was last modified) and does not seem to be compatible with rails 3.2

What I tried then:

- 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 ?

解决方案

ActiveRecord works OK with legacy databases. I did a back-end system that didn't use Rails with ActiveRecord as my ORM. "ActiveRecord Without Rails" got me started. "Using ActiveRecord outside Rails" is also useful. Search Google for "use activerecord without rails" and you'll find even more.

You don't need a fully fleshed out model. Just use a base class for the tables you want and ActiveRecord will query the database for what it needs. It won't know about table relationships, but for general queries it'll do fine. Build the relationships as you go and need them.

这篇关于接轨现有的Postgres数据库 - 不会看到控制台和控制器型号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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