So far we have completed the “Thought Log” application and taken a look at how RJS fits into the Rails framework. Now it is time to examine an example that is a bit more realistic and solves some of the problems that you might actually run into in your own projects. My expenses have been getting out of hand lately, so let's build a simple application to help track them.
First, we'll run the Rails model generator to create the models used throughout this project. The Rails model generator automatically creates stub files for the models and database migrations. Then we'll edit the generated files to add our own functionality.
expenses> ruby script/generate model Project exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/project.rb create test/unit/project_test.rb create test/fixtures/projects.yml create db/migrate create db/migrate/001_create_projects.rb expenses> ruby script/generate model Expense exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/expense.rb create test/unit/expense_test.rb create test/fixtures/expenses.yml exists db/migrate create db/migrate/002_create_expenses.rb
The generator creates the Project model in
app/models/project.rb and the Expense model in
app/models/expense.rb, along with unit test stubs and
test fixtures. The generator also created two migrations for us:
Now that the model ...