从现有的表创建模型

从现有的表创建模型

本文介绍了扶手:从现有的表创建模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不同的项目已经创建的表。他们的名字被格式化像aaa_bbb_ccc_ddd(所有非复数,有些部分是不是约定字)。我已经成功通过读this.但现在我必须做出实际的模型。我看着 RMRE ,但他们执行的ActiveRecord约定在我的表,改变他们的名字,我不想这样做,因为其他应用程序依赖于这些表。

I have tables already created from a different project. Their names are formatted like aaa_bbb_ccc_ddd (all non plural and some parts aren't a convention word). I have successfully created a schema from the database by reading this. But now I have to make the actual models. I've looked at RMRE, but they enforce the ActiveRecord convention on my tables and change their names, which I don't want to do because other apps depend on those tables.

什么是自动创建模型的最佳方式,并从现有表的模式?

What is the best way to automatically create models and a schema from existing tables?

推荐答案

只是一个理论,不知道如何做到这一点的工作在真正的应用程序:

just a theory, not sure how this would work in real app:

创建模式命名为的ActiveRecord 公约要求,例如用于表 aaa_bbb_ccc_ddd 您将创建一个模型 AAABBB 键,这个模型映射到表:

create models named as ActiveRecord convention requires, for example for table aaa_bbb_ccc_ddd you'll create a model AaaBbb and map this model to your table:

class AaaBbb < ActiveRecord::Base
    self.table_name = "aaa_bbb_ccc_ddd"
end

或更人性化的例子:

or a more human example:

class AdminUser < ActiveRecord::Base
    self.table_name = "my_wonderfull_admin_users"
end

现在你将有 AAABBB 作为资源的路线这意味着你有一个像网址:

Now you'll have AaaBbb as resource in routes meaning you'll have a url like:

 .../aaa_bbb/...

如果你想使用的表名名的URL我猜你可以重写的路线:

and if you want to use the table name name in url I guess you could rewrite the route:

得到'aaa_bbb_ccc_ddd /:身份证',aaa_bbb#秀,如:aaa_bbb

再次,只是一个理论,可能会帮助你。没有这样的情况下工作,还没有,但会已经开始从这个。

again, just a theory that might help you out. Didn't work with such cases yet but would've start from this.

修改

自动化模型创建的数据库:

https://github.com/bosko/rmre

但我认为这将创建与奇怪的名字轨约定的模型,你必须为资源使用您的应用程序。

but I think this will create models by rails convention with wierd names that you'll have to use as resource in your app.

有一个很好的模板,我发现所以,如果你想用从表名称不同的型号名称:

A good template that I found on SO in case you want to use a model name different from table name:

class YourIdealModelName < ActiveRecord::Base
  self.table_name = `actual_table_name`
  self.primary_key = `ID`

  belongs_to :other_ideal_model,
    :foreign_key => 'foreign_key_on_other_table'

  has_many :some_other_ideal_models,
    :foreign_key => 'foreign_key_on_this_table',
    :primary_key => 'primary_key_on_other_table'
end

这篇关于扶手:从现有的表创建模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 07:37