1

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

2
  • Found duplicate- stackoverflow.com/questions/8919170/… However, I don't see the -c bit? Commented Jan 20, 2012 at 19:47
  • That question is different. You're talking about your class being empty, and that question was about the controller being empty. Commented Jan 20, 2012 at 19:56

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.