简体   繁体   English

滑轨生成支架不会生成模型

[英]Running rails generate scaffold does not generate model

If I type (copy / paste exactly from "rails g scaffold --help") 如果输入(从“ rails g scaffold --help”复制/粘贴)

rails generate scaffold purchase amount:decimal tracking_id:integer:uniq

Then the controller is created, views, the model is created.. but it contains no properties. 然后创建控制器,创建视图,创建模型..但是它不包含任何属性。 It literally contains: 它实际上包含:

class Purchase < ActiveRecord::Base
end

Am I missing something? 我想念什么吗?

Versions 版本号
Rails 3.2.0 Rails 3.2.0
ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0] 红宝石1.8.7(2010-01-10补丁程序级别249)[universal-darwin11.0]
Mac OSX Lion Mac OSX狮子

That's actually right. 没错 Normally if you were making some random Ruby program and you made a class, you'd probably want to throw in some instance variables and such, but that's now how it works in Rails. 通常,如果您正在制作一些随机的Ruby程序并创建了一个类,则可能要抛出一些实例变量,诸如此类,但是现在这就是它在Rails中的工作方式。 A model is both the class and the database table for it. 模型既是类又是数据库表。

In db/migrate you'll see the migration file that made your Purchase table in your database, and inside you'll see that it generates the columns you asked for. db/migrate您将看到在数据库中构成“购买”表的迁移文件,在内部,您将看到它生成了您要求的列。 When you save data to the database, you're saving an instanced object (in general). 将数据保存到数据库时,通常是在保存实例对象。

Open up Rails Console (type rails console in to your terminal) and try this: 打开Rails Console(在您的终端中键入rails console ),然后尝试以下操作:

Purchase.count
Purchase.create!(:tracking_id => 1)
Purchase.count
my_purchase = Purchase.first
my_purchase.tracking_id

You'll see that you have 0 purchase objects/rows in the database at first. 您会首先发现数据库中有0个购买对象/行。 Then you can create one, and pass in a value for your instance variable (the tracking id). 然后,您可以创建一个,并为您的实例变量传递一个值(跟踪ID)。 When you check the count again, you'll see 1. When you grab the first (and only) item in the item, you'll be able to use the dynamic tracking_id method as an accessor. 再次检查计数时,将看到1。当您抓住项目中的第一个(也是唯一的)项目时,您将能够使用dynamic tracking_id方法作为访问器。

I suggest you read up on Rails more in general to learn more about why this is right and what is going on. 我建议您在Rails上更全面地阅读,以更多地了解为什么这是正确的以及正在发生的事情。

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

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