扶手:从现有的表创建模型?

编程入门 行业动态 更新时间:2024-10-27 14:31:57
本文介绍了扶手:从现有的表创建模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个不同的项目已经创建的表。他们的名字被格式化像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.

修改

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

github/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

更多推荐

扶手:从现有的表创建模型?

本文发布于:2023-10-20 12:57:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1511030.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:扶手   模型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!