使用ActiveRecord的为未命名为复数表 [英] Using ActiveRecord for tables that aren't named as plurals

查看:374
本文介绍了使用ActiveRecord的为未命名为复数表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个基本的Rails应用程序读取数据从一个MS SQL Server数据库。在我读(下剪断)的教程,它提到该表需要遵循一定的规则,如名称是一个多元化,并强调,等等等等没有一个表在我的数据库中按照惯例,而且可以吨被改变。这仍然是真的吗?如果是的话,做什么,我需要看的懂如何与这些表进行通信?

SNIPPET 的http://coding.smashingmagazine.com/2009/03/27/ultimate-beginners-guide-to-ruby-on-rails/

结构,你的模型的行为和数据库中的表之间的映射是完全自动化的居多,只要你遵守一些规则,你的表的设计。总的原则是:

  1. 您的表名是复数,强调你的类名的变种。所以,一类用户将被映射到表users.CookieRecipe将被映射到cookie_recipes

  2. 您的表需要有名为id的自动递增属性整数列的主键。

  3. 关联到其他表存储在被他们命名列。如果你想存储属于用户的帖子,职位表都需要user_id列

解决方案

首先,假设你有一些其他的数据库系统,支撑您的Rails应用程序,而你只需要读入到SQLServer数据库唯一获得补充。

首先需要在app /模型/ rosql.rb主模型:

 高清Rosql<的ActiveRecord :: Base的
   establish_connection(ROSQL#{Rails.env})
结束
 

然后将其添加到您的database.yml

  ROSQLproduction:
  适配器:SQLSERVER
  模式:DBLIB
  数据服务器:不管
  用户名:用​​户
  密码:密码
  数据库:不管
  端口:1433
 

这最后一部分实际上是高度依赖你用它来访问SQL Server中的堆栈。以上是我特别的选择,这是TinyTDS / freetds的的一个例子。无论如何,现在可以开始宣布SQL Server的表款,像这样:

  SqlServerTable< Rosql
  self.table_name =non_railsy_table_name
结束
 

您可能会或可能不会想尝试和定义主键:

  SqlServerTable< Rosql
  self.table_name =non_railsy_table_name
  self.primary_keythingKey_PK
结束
 

所以,现在,假设thingKeyPK是一个字符串!

  @x = SqlServerTable.find(XYZ123)
 

将工作!

I'm trying to get a basic rails app to read data out of a MS SQL Server database. In the tutorial I'm reading (snipped below), it mentions that the tables need to follow certain rules, such as the name being a pluralized and underscored, etc etc. None of the tables in my database follow that convention and that can't be changed. Is this still true? If so, what do I need to read to understand how to communicate with such tables?

SNIPPET http://coding.smashingmagazine.com/2009/03/27/ultimate-beginners-guide-to-ruby-on-rails/

Mappings between the structure and behavior of your models and the tables in the database are fully automated mostly, provided that you adhere to a few rules in the design of your tables. The general principles are:

  1. Your table name is the pluralized, underscored variant of your classname. So, a class User would be mapped to the table users.CookieRecipe would be mapped to cookie_recipes.

  2. Your table needs to have its primary key on an integer column called id with the auto increment property.

  3. Associations to other tables are stored in columns that are named after them. If you want to store posts belonging to a user, the posts table would need a user_id column

解决方案

First, assume you have some other database system underpinning your rails app, and you just need to add in read only access to the SQLServer database.

First create a master model in app/models/rosql.rb:

def Rosql < ActiveRecord::Base
   establish_connection("ROSQL#{Rails.env}")
end

Then add this to your database.yml

ROSQLproduction:
  adapter: sqlserver
  mode: dblib
  dataserver: WHATEVER
  username: user
  password: password
  database: WHATEVER
  port: 1433

That last part is actually highly dependent on the stack you use to access SQL Server. Above is an example of my particular choice, which is TinyTDS/Freetds. Regardless, now you can start declaring SQL Server table models like so:

SqlServerTable < Rosql
  self.table_name = "non_railsy_table_name"
end

You may or may not want to try and define the primary key:

SqlServerTable < Rosql
  self.table_name = "non_railsy_table_name"
  self.primary_key "thingKey_PK"
end

So now, assume thingKeyPK is a string!

@x = SqlServerTable.find("XYZ123")

will work!

这篇关于使用ActiveRecord的为未命名为复数表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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