Running rails generate scaffold does not generate model

丶灬走出姿态 提交于 2019-12-24 09:58:45

问题


If I type (copy / paste exactly from "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
ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]
Mac OSX Lion


回答1:


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. 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. 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:

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. Then you can create one, and pass in a value for your instance variable (the tracking 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.

I suggest you read up on Rails more in general to learn more about why this is right and what is going on.



来源:https://stackoverflow.com/questions/8946980/running-rails-generate-scaffold-does-not-generate-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!