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.

112 CHAPTER 5 Nested resources<br />

The solution is the second value for this option:<br />

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

This simply deletes all the tickets using a SQL delete, like this:<br />

DELETE FROM tickets WHERE project_id = :project_id<br />

This operation is quick and is exceptionally useful if you have a large number of tickets<br />

that don’t have callbacks. If you do have callbacks on Ticket for a destroy operation,<br />

then you should use the first option, :dependent => :destroy.<br />

Finally, if you just want to disassociate tickets from a project and unset the<br />

project_id field, you can use this option:<br />

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

When a project is deleted with this type of :dependent option defined, it will execute<br />

an SQL query such as this:<br />

UPDATE tickets SET project_id = NULL WHERE project_id = :project_id<br />

Rather than deleting the tickets, this option keeps them around, but their project_id<br />

fields are unset.<br />

Using this option would be helpful, for example, if you were building a tasktracking<br />

application and instead of projects and tickets you had users and tasks. If you<br />

delete a user, you may want to reassign rather than delete the tasks associated with that<br />

user, in which case you’d use the :dependent => :nullify option instead.<br />

In your projects and tickets scenario, though, you use :dependent => :destroy if<br />

you have callbacks to run on tickets when they’re destroyed or :dependent =><br />

:delete_all if you have no callbacks on tickets.<br />

This was a little bit of a detour for the work you’re doing now, but it’s a nice thing<br />

to know if you ever need to delete an associated object when the original object is<br />

deleted.<br />

Let’s look at how to edit the tickets in your application.<br />

5.3 Editing tickets<br />

You want users to be able to edit tickets, the updating part of this CRUD interface. This<br />

section covers creating the edit and update actions for the TicketsController.<br />

The next feature you’re going to implement is the ability to edit tickets. This functionality<br />

follows a thread similar to the projects edit feature where you follow an Edit<br />

link in the show template. With that in mind, you can write this feature using the code<br />

in the following listing and put it in a file at features/editing_tickets.feature.<br />

Listing 5.12 features/editing_tickets.feature<br />

Feature: Editing tickets<br />

In order to alter ticket information<br />

As a user<br />

I want a form to edit the tickets

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

Saved successfully!

Ooh no, something went wrong!