简体   繁体   English

从Rails 2中的现有表模型

[英]Model from existing table in Rails 2

I have a database with tables. 我有一个带表格的数据库。 I want to create a model in my Rails app from existing table. 我想在现有表格的Rails应用程序中创建一个模型。 As i know, such functionality is available, and is done as follows: 据我所知,这样的功能是可用的,并按如下方式完成:

script/generate scaffold model_name --skip-migration


Of course, i defined my database in database.yml file. 当然,我在database.yml文件中定义了我的数据库。 Scaffold generated for me a model with controller and views. 脚手架为我生成了一个带控制器和视图的模型。 My table name is not as it must be for Rails(it is incorrect, not following conventions), i added set_table_name to my controller. 我的表名不是Rails必须的(它不正确,不遵循约定),我将set_table_name添加到我的控制器。 But, when i am calling the index method, on my page i have only set of # symbols, but not a data from database. 但是,当我调用索引方法时,在我的页面上我只有#符号集,但不是数据库中的数据。 In my index.html.erb i have only generated code by scaffold. 在我的index.html.erb中,我只通过scaffold生成代码。 How can i print out my database data? 如何打印出我的数据库数据?

Have you generated a schema file from your existing database? 您是否从现有数据库生成了模式文件? If you run the command 如果您运行该命令

rake db:schema:dump

and then re-generate your scaffold this should fix the problem. 然后重新生成你的脚手架这应该解决问题。

Additionally you may wish to check out Dr Nic's Magic Model generator . 此外,您可以查看Nic博士的魔术模型生成器 This will generate models for all of your existing tables and attempt to guess the relationships. 这将为所有现有表生成模型,并尝试猜测关系。 This will probably not work if your table naming is not understandable by rails. 如果rails无法理解您的表命名,这可能不起作用。

UPDATE UPDATE

I do not generally use the default scaffold however I have tested this myself and it appears that if you skip the migration and do not pass any column name/type pairs then the scaffold generator will not create anything in the template to render the columns. 我一般不使用默认的脚手架,但是我自己测试了这个,看来如果你跳过迁移并且没有传递任何列名/类型对,那么脚手架生成器将不会在模板中创建任何内容来呈现列。

You have two choices here either 你有两个选择

  1. Pass in the column name pairs as well as skip-migration or 传入列名称对以及跳过迁移或
  2. Download Ryan Bates Nifty Scaffold generator which will create the scaffold with the column names even if you specify --skip-migration 下载Ryan Bates Nifty脚手架生成器 ,即使您指定了--skip-migration,也会创建带有列名称的脚手架

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM