27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

5.1.3 Defining a has_many association<br />

Creating tickets<br />

The tickets method is defined by an association method in the Project class called<br />

has_many, which you can use as follows, putting it directly above the validation you put<br />

there earlier:<br />

has_many :tickets<br />

As mentioned before, this defines the tickets method you need but also gives you a<br />

whole slew of other useful methods, such as the build method, which you call on the<br />

association. The build method is equivalent to new for the Ticket class (which you<br />

create in a moment) but associates the new object instantly with the @project object<br />

by setting a foreign key called project_id automatically. Upon running the feature,<br />

you get this:<br />

And I follow "New Ticket"<br />

uninitialized constant Project::Ticket (NameError)<br />

You can determine from this output that the method is looking for the Ticket class,<br />

but why? The tickets method on Project objects is defined by the has_many call in<br />

the Project model. This method assumes that when you want to get the tickets, you<br />

actually want objects of the Ticket model. This model is currently missing; hence, the<br />

error. You can add this model now with the following command:<br />

rails generate model ticket title:string description:text project:references<br />

The project:references part defines an integer column for the tickets table called<br />

project_id in the migration. This column represents the project this ticket links to<br />

and is called a foreign key. You should now run the migration by using rake<br />

db:migrate and load the new schema into your test database by running rake<br />

db:test:prepare.<br />

The rake db:migrate task runs the migrations and then dumps the structure of<br />

the database to a file called db/schema.rb. This structure allows you to restore your<br />

database using the rake db:schema:load task if you wish, which is better than running<br />

all the migrations on a large project again! The rake db:test:prepare task<br />

loads this schema into the test database, making the fields that were just made available<br />

on the development database by running the migration also now available on<br />

the test database.<br />

Now when you run bin/cucumber features/creating_tickets.feature, you’re<br />

told the new template is missing:<br />

And I follow "New Ticket"<br />

Missing template tickets/new, application/new<br />

with {:handlers=>[:erb, :builder],<br />

:formats=>[:html],<br />

:locale=>[:en, :en]}.<br />

Searched in:<br />

* ".../ticketee/app/views"<br />

A file seems to be missing! You must create this file in order to continue.<br />

103

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!