# Trouble passing :id of one model to another model

**URL:** <https://www.ruby-forum.com/t/trouble-passing-id-of-one-model-to-another-model/130617>\
**Category:** Rails\
**Created:** [February 29, 2008, 9:55pm UTC](https://www.ruby-forum.com/t/trouble-passing-id-of-one-model-to-another-model/130617 "2008-02-29T21:55:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![loominator1970](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6f9a4e/32.png) [@loominator1970](https://www.ruby-forum.com/u/loominator1970)\
**Post date:** [February 29, 2008, 9:55pm UTC](https://www.ruby-forum.com/t/trouble-passing-id-of-one-model-to-another-model/130617/1 "2008-02-29T21:55:52Z")

</div>

Hello,

I’m having trouble workign with two models, specifically passing the  
id of my “Tools” model to New controller of my task model.

I am creating a page that keeps track of tools we are building in our  
group. For each tool, there can be several tasks associated with  
completing the code/process of the tool. I have a model for tool  
(“tools”) and a model for tasks (“tooltasks”). On the tools view,  
each tool is listed out in table format. Next to each tool is a  
button/link called “add task”. When the user clicks this button, I  
would like to bring up the tooltasks#new view with the tool.id (or  
just tool passed to the form). This is where its not working and i’m  
stuck. Any help out there?

offending code:

—\> tools main page = “index.html.erb”

\<% for tool in @tools %\> "\> \<%=link\_to tool.title, tool %\> \<%=h tool.description %\> \<%=h tool.status %\> \<%=h tool.priority %\> \<%=h tool.owner %\> \<%=h tool.eta %\> \<%= button\_to 'Add Task', new\_tooltask\_path,:id =\> tool.id %\> \<%= button\_to 'Edit', edit\_tool\_path(tool) %\> \<%= button\_to 'Destroy', tool, :confirm =\> 'Are you sure?', :method =\> :delete %\> \<% end %\>

tooltasks\_controller.rb  
def new  
@tooltask = Tooltask.new

```
     # is this the problem????
     @tool = Tool.find(params[:id])

     respond_to do |format|
       format.html # new.html.erb
       format.xml { render :xml => @tooltask }
     end
 end

```

What’s going on? What am I missing? thanks for any help

Dave

---

<div class="post-metadata">

**Author:** ![loominator1970](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6f9a4e/32.png) [@loominator1970](https://www.ruby-forum.com/u/loominator1970)\
**Post date:** [February 29, 2008, 10:31pm UTC](https://www.ruby-forum.com/t/trouble-passing-id-of-one-model-to-another-model/130617/2 "2008-02-29T22:31:59Z")

</div>

On 2/29/08, loominator1970 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> each tool is listed out in table format. Next to each tool is a
> 
> \<tr valign="top" class="\<%=cycle("tool-line-odd","tool-line-even") \<%=h tool.owner \<% end %\>
> 
> ```
> respond_to do |format|
> format.html # new.html.erb
> format.xml { render :xml => @tooltask }
> end
> end
> 
> ```

what’s the error that’s produced? What’s the backtrace of the error?  
What does the error message indicate is the offending line? I assume  
you’re getting a ActiveRecord::RecordNotFound exception. What are the  
params being passed to your TooltasksController#new method? I’m  
guessing that params[:id] is actually nil…

Try using:

new\_tooltask\_path(:tool\_id =\> tool.id)

and in your controller:

@tool = Tool.find\_by\_id(params[:tool\_id])

also remember to never call Class.find() without enclosing it within a  
begin/rescue block, otherwise you’ll raise an exception if no record  
is found. Or use find\_by\_id and check to make sure that it actually  
returns a record.

You could also look into nested routes if you want to make your urls  
look prettier, since the above will generate something like:

tooltask/new?tool\_id=1234

whereas with a nested route, you could use something like

tools/1234/tasks/new

Mike

---

<div class="post-metadata">

**Author:** ![loominator1970](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6f9a4e/32.png) [@loominator1970](https://www.ruby-forum.com/u/loominator1970)\
**Post date:** [February 29, 2008, 11:04pm UTC](https://www.ruby-forum.com/t/trouble-passing-id-of-one-model-to-another-model/130617/3 "2008-02-29T23:04:59Z")

</div>

Works like a dream! thanks Mike, and thanks for the advice!

Dave
