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.

Viewing tickets<br />

The root_path method is made available by the call to the root method in config/<br />

routes.rb. This simply outputs / when it’s called, providing a path to the root of your<br />

application.<br />

Running bin/cucumber features/viewing_tickets.feature again, you can see<br />

this is all working:<br />

1 scenario (1 passed)<br />

18 steps (18 passed)<br />

Your code expressly states that inside the TextMate 2 project, you should see only the<br />

"Make it shiny!" ticket, and inside the Internet Explorer project, you should see only<br />

the "Standards compliance" ticket. Both statements worked.<br />

Time to make sure everything else is still working by running rake cucumber:ok<br />

spec. You should see that everything is green:<br />

10 scenarios (10 passed)<br />

80 steps (80 passed)<br />

# and<br />

5 examples, 0 failures, 4 pending<br />

Fantastic! Push!<br />

git add .<br />

git commit -m "Implemented features for displaying a list of relevant<br />

tickets for projects and viewing particular tickets"<br />

git push<br />

Now you can see tickets just for a particular project, but what happens when a project<br />

is deleted? The tickets for that project would not be deleted automatically. To implement<br />

this behavior, you can pass some options to the has_many association, which will<br />

delete the tickets when a project is deleted.<br />

5.2.2 Culling tickets<br />

When a project is deleted, its tickets become useless: they’re inaccessible because of<br />

how you defined their routes. Therefore, when you delete a project, you should also<br />

delete the tickets for that project, and you can do that by using the :dependent option<br />

on the has_many association defined in your Project model.<br />

This option has three choices that all act slightly differently from each other. The<br />

first one is the :destroy value:<br />

has_many :tickets, :dependent => :destroy<br />

If you put this in your Project model, any time you call destroy on a Project object,<br />

Rails iterates through each ticket for this project and calls destroy on them, then calls<br />

any destroy callbacks (such as any has_manys in the Ticket model, which also have the<br />

dependent option) 3 the ticket objects have on them, any destroy callbacks for those<br />

objects, and so on. The problem is that if you have a large number of tickets, destroy<br />

is called on each one, which will be slow.<br />

3 Or any callback defined with after_destroy or before_destroy.<br />

111

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

Saved successfully!

Ooh no, something went wrong!