# RecordNotFound - I'm not understanding the

**URL:** <https://www.ruby-forum.com/t/recordnotfound-im-not-understanding-the/105937>\
**Category:** Rails\
**Created:** [June 4, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/recordnotfound-im-not-understanding-the/105937 "2007-06-04T20:30:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Big\_Dave\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9bcb4/32.png) [@Big\_Dave\_S](https://www.ruby-forum.com/u/Big_Dave_S)\
**Post date:** [June 4, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/recordnotfound-im-not-understanding-the/105937/1 "2007-06-04T20:30:53Z")

</div>

Hello! I’m new to rails ad ActiveRecord, and have a very basic  
problem.

I have a very basic rails app, which I want to contain computers, and  
notes. Each computer has associated notes. I’m getting an error I’m  
not fully understanding when I try to view a computer. The view  
should include a listing of notes for the associated computer, but  
instead, I get this:

Couldn’t find Note without an ID

The link that takes the user to the screen with the problem:  
\<%= link\_to ‘Show’, :action =\> ‘show’, :id =\> computer %\>

My controller:  
def show  
@computer = Computer.find(params[:id])  
@notes = Note.find(params[:computer\_id])  
end

My view for show:  
\<% for column in Computer.content\_columns %\>

**\<%= column.human\_name %\>:** \<%=h @computer.send(column.name) %\>

\<% end %\> \<% for note in @notes %\> \<% for column in Note.content\_columns %\> \<% end %\> \<% end %\>

| \<%=h note.send(column.name) %\> |

\<%= link\_to ‘Edit’, :action =\> ‘edit’, :id =\> @computer %\> |  
\<%= link\_to ‘Back’, :action =\> ‘list\_computers’ %\>

I realize I’m not setting :computer\_id anywhere, but using :id in my  
controller for notes gives me the same issue, and I don’t want to pull  
up notes with a particular id, but notes with a particular associated  
computer. It seems like there should be a simple way to accomplish  
this in rails, and I’m just missing it.

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![Big\_Dave\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9bcb4/32.png) [@Big\_Dave\_S](https://www.ruby-forum.com/u/Big_Dave_S)\
**Post date:** [June 4, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/recordnotfound-im-not-understanding-the/105937/2 "2007-06-04T20:39:59Z")

</div>

On Jun 4, 2007, at 2:29 PM, Big Dave S. wrote:

> My controller:  
> def show  
> @computer = Computer.find(params[:id])  
> @notes = Note.find(params[:computer\_id])  
> end

Presuming you have models along the lines of:

class Computer \< ActiveRecord::Base  
has\_many :notes  
end

class Note \< ActiveRecord::Base  
belongs\_to :computer  
end

What you probably want for the above is:

def show  
@computer = Computer.find(params[:id], :include =\> :notes)  
@notes = @computer.notes  
end

Or if you wanted to search the notes without actually loading the  
Computer, and params[:id] included the computer id, you’d want:

def show  
@notes = Note.find\_by\_computer\_id(params[:id])  
end

If you haven’t read Agile Web D. With Ruby on Rails or one  
of the other rails books, it’d definitely be worth your while to do  
so, to get a better understanding of how rails handles parameters,  
and associations.

James.

---

<div class="post-metadata">

**Author:** ![Big\_Dave\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9bcb4/32.png) [@Big\_Dave\_S](https://www.ruby-forum.com/u/Big_Dave_S)\
**Post date:** [June 4, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/recordnotfound-im-not-understanding-the/105937/3 "2007-06-04T20:41:59Z")

</div>

Cheers,  
Jodi  
General Partner  
The nNovation Group inc.  
www.nnovation.ca/blog

On 4-Jun-07, at 2:29 PM, Big Dave S. wrote:

> ```
> @notes = Note.find(params[:computer_id])
> 
> ```

Hey Big Dave,

Note.find() is a finder that retrieves the row with the provided  
primary key (so Note.find(1) will retrieve note with pk of 1).

To query all notes,

@notes = Note.find(:all, :conditions =\> [“computer\_id = ?”, params  
[:computer\_id])

or better yet, since you’ve just retrieved the computer, then this  
the way to go:

@notes = @computer.notes

sweet eh?

(as James suggested, pickup Pickaxe (er, ‘skateboard’). It’ll get you  
over a bunch of early humps)

Jodi

---

<div class="post-metadata">

**Author:** ![Big\_Dave\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9bcb4/32.png) [@Big\_Dave\_S](https://www.ruby-forum.com/u/Big_Dave_S)\
**Post date:** [June 4, 2007, 9:07pm UTC](https://www.ruby-forum.com/t/recordnotfound-im-not-understanding-the/105937/4 "2007-06-04T21:07:04Z")

</div>

On Jun 4, 2007, at 2:43 PM, Jodi S. wrote:

> small correction -
> 
> > @notes = Note.find\_by\_computer\_id(params[:id])
> 
> will return one row. Note.find\_all\_by\_computer\_id will do a find(:all)

Yeah, sorry – trying to get a response sent out quickly before  
getting back to work.

James.

---

<div class="post-metadata">

**Author:** ![Big\_Dave\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9bcb4/32.png) [@Big\_Dave\_S](https://www.ruby-forum.com/u/Big_Dave_S)\
**Post date:** [June 4, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/recordnotfound-im-not-understanding-the/105937/5 "2007-06-04T20:44:00Z")

</div>

On 4-Jun-07, at 2:38 PM, James S. wrote:

> > The link that takes the user to the screen with the problem:  
> > class Computer \< ActiveRecord::Base  
> > @computer = Computer.find(params[:id], :include =\> :notes)  
> > If you haven’t read Agile Web D. With Ruby on Rails or one  
> > of the other rails books, it’d definitely be worth your while to do  
> > so, to get a better understanding of how rails handles parameters,  
> > and associations.
> 
> James.

small correction -

> @notes = Note.find\_by\_computer\_id(params[:id])

will return one row. Note.find\_all\_by\_computer\_id will do a find(:all

see my forthcoming announcement on find\_by\_associations to clean this  
up even more

Jodi

---

<div class="post-metadata">

**Author:** ![Big\_Dave\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9bcb4/32.png) [@Big\_Dave\_S](https://www.ruby-forum.com/u/Big_Dave_S)\
**Post date:** [June 4, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/recordnotfound-im-not-understanding-the/105937/6 "2007-06-04T21:27:10Z")

</div>

Big Dave S. wrote:

> The link that takes the user to the screen with the problem:  
> \<%= link\_to ‘Show’, :action =\> ‘show’, :id =\> computer %\>

I think you mean :id =\> @computer in the above line

–  
Cheers,

- Jacob A.
