Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Clair Griffiths
Clair Griffiths
10,158 Points

params[:id] question

When I have a single controller (i.e. TodoList) it looks like I'm able to find the list in question just using :id

@todo_list = TodoList.find(params[:id])

However, when I have another related controller (i.e. TodoItems) and I need to find a list to then find an item, this time I seem to need to use todo_list_id instead.

@todo_list = TodoList.find(params[:todo_list_id])
@todo_item = @todo_list.todo_items.find(params[:id])

Is there an technical reason for this or is it just to make it clearer for the second find that I run to create the todo_item?

Many thanks in advance!

3 Answers

Clair Griffiths
Clair Griffiths
10,158 Points

Thanks Charles!

I'm afraid I'm still confused though, I didn't word my question correctly, so apologies!

My (reworded!) question is, why can't I use the following code:

@todo_list = TodoList.find(params[:id])
@todo_item = @todo_list.todo_items.find(params[:id])

In the tutorial it is suggested that I use (params[:todo_list_id) rather than (params[:id]) when I'm assigning to the variable @todo_list. In my understanding, it is exactly the same way that I assigned a specific todo list to @todo_list when I was creating it in the TodoList controller, so why the need for the change when I'm in the TodoItem controller? Is it for clarity (there are a lot of :id's floating around at this point!) or is it to stop the code from breaking?

Many thanks again in advance!

Hey no probs! Alright, let's break this down.

In the TodoList controller, the :id corresponds to the ID of a specific todo list. In the TodoItem controller, the :id corresponds to the ID of a specific todo item.

In your example:

@todo_list = TodoList.find(params[:id])
@todo_item = @todo_list.todo_items.find(params[:id])

If we used the :id of the todo item to search for a todo list, we most likely will get the wrong list since :id in the Todo Item controller corresponds to a given Todo Item ID and not a Todo List ID.

Instead, we can pass along the specific TodoList ID (:todo_list_id) along with a TodoItem ID (:id) to the Todo Item controller to ensure that we're getting the correct list based off the :todo_list_id and the correct Todo Item based off the :id.

I may be belaboring the point here but just to make sure.

# In the TodoList Controller
# We're looking for a todo list by a todo list ID. 
@todo_list = TodoList.find(params[:id])
# In the Todo Item Controller
# Trying to search for a todo list based on the todo item ID.
@todo_list = TodoList.find(params[:id])

# Correctly searching for a todo list based on the todo list ID.
@todo_list = TodoList.find(params[:todo_list_id])

# Now correctly searching for a todo item using the todo item ID within the previously searched for todo list. 
@todo_item = @todo_list.todo_items.find(params[:id])

Hope this helps!

Awesome question!

Here's the breakdown.

@todo_list = TodoList.find(params[:todo_list_id])
@todo_item = @todo_list.todo_items.find(params[:id])

In this example we could've easily done (or something similar):

@todo_item = TodoItems.find(params[:id])

This should work, but now we're searching through ALL of the todo list items instead of just those belonging to a specific TodoList.

By specifying a specific TodoList, we don't slow down our search performance by only searching through the neccessary TodoListItems.

Hope this helps!

Clair Griffiths
Clair Griffiths
10,158 Points

Amazing! This is what I thought might happen but I wasn't sure. Makes total sense to me now.

Thank you so much for taking the time to answer (twice!)