Reputation: 38529
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
Upvotes: 1
Views: 1751
Reputation: 11647
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.
Upvotes: 3