# Use find with a variable

**URL:** <https://discuss.rubyonrails.org/t/use-find-with-a-variable/60578>\
**Category:** rubyonrails-talk\
**Created:** [August 25, 2011, 4:34pm UTC](https://discuss.rubyonrails.org/t/use-find-with-a-variable/60578 "2011-08-25T16:34:38Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [August 25, 2011, 4:34pm UTC](https://discuss.rubyonrails.org/t/use-find-with-a-variable/60578/1 "2011-08-25T16:34:38Z")

</div>

I got the id of a "part" in variable "the\_id". In a partial "\_order\_fields.html.erb"

So I do this... @a = Part.find(the\_id)

I get this error... Couldn't find Part without an ID

So I also try... @a = Part.find(the\_id.to\_i)

Get same error.

If I do @a = Part.find(2), it works. But I need to do the "find" using a variable, instead of hard-coding the value.

(I know I shouldn't be doing this in a view and I know the code is ugly by Ruby programming standards but I'm testing to see if it works, then I'll rename it)

---

<div class="post-metadata">

**Author:** ![Michael\_Pavling](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Pavling](https://discuss.rubyonrails.org/u/Michael_Pavling)\
**Post date:** [August 25, 2011, 4:40pm UTC](https://discuss.rubyonrails.org/t/use-find-with-a-variable/60578/2 "2011-08-25T16:40:01Z")

</div>

What value does "the\_id" have if you add a breakpoint before the find and inspect it?

---

<div class="post-metadata">

**Author:** ![Jeffrey](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@Jeffrey](https://discuss.rubyonrails.org/u/Jeffrey)\
**Post date:** [August 25, 2011, 4:47pm UTC](https://discuss.rubyonrails.org/t/use-find-with-a-variable/60578/3 "2011-08-25T16:47:12Z")

</div>

Quoting Leonel \*.\* \<lists@ruby-forum.com\>:

> I got the id of a "part" in variable "the\_id". In a partial "\_order\_fields.html.erb"
> 
> So I do this... @a = Part.find(the\_id)
> 
> I get this error... Couldn't find Part without an ID

Uhh, are you sure the\_id has a numeric value at that point in the view? You don't say where or how it is set. Local controller variables are not shared with views, though controller instance variables are. And anything can be passed to a partial, though not regular view through the :locals hash.

HTH, &nbsp;&nbsp;Jeffrey

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [August 25, 2011, 4:49pm UTC](https://discuss.rubyonrails.org/t/use-find-with-a-variable/60578/4 "2011-08-25T16:49:13Z")

</div>

Ah! found the solution: should've used "find\_by\_id", instead of just "find".

> <https://stackoverflow.com/questions/1593120/variable-passing-to-find-function-in-rails-2>

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [August 25, 2011, 5:04pm UTC](https://discuss.rubyonrails.org/t/use-find-with-a-variable/60578/5 "2011-08-25T17:04:38Z")

</div>

So, I do find the "part", using a = Part.find\_by\_id(the\_id)

If I do \<%= debug a %\> I get...

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [August 25, 2011, 5:13pm UTC](https://discuss.rubyonrails.org/t/use-find-with-a-variable/60578/6 "2011-08-25T17:13:34Z")

</div>

This is very weird, even if I do a.methods, "cost" is there!

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [August 25, 2011, 5:18pm UTC](https://discuss.rubyonrails.org/t/use-find-with-a-variable/60578/7 "2011-08-25T17:18:40Z")

</div>

This is making me crazy, even if I do... \<%= a %\>

I get... #\<Part:0x7f2fe2bf4cf8\>

But as soon as I try to access the cost or name attributes I get... undefined method `cost' for nil:NilClass

and it's NOOT NIIILL!!!

---

<div class="post-metadata">

**Author:** ![Jeremiah\_Dodds](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@Jeremiah\_Dodds](https://discuss.rubyonrails.org/u/Jeremiah_Dodds)\
**Post date:** [August 25, 2011, 11:01pm UTC](https://discuss.rubyonrails.org/t/use-find-with-a-variable/60578/8 "2011-08-25T23:01:44Z")

</div>

Can you pastebin your controller / view code?

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [August 26, 2011, 2:16am UTC](https://discuss.rubyonrails.org/t/use-find-with-a-variable/60578/9 "2011-08-26T02:16:54Z")

</div>

Leonel \*.\* wrote in post #1018495:

> So, I do find the "part", using a = Part.find\_by\_id(the\_id)
> 
> If I do \<%= debug a %\> I get... ---------------------- attributes: &nbsp;&nbsp;name: Western Digital - Caviar GP 500GB Internal Hard Drive &nbsp;&nbsp;created\_at: 2011-08-23 18:54:12.214873 &nbsp;&nbsp;cost: 49.99 &nbsp;&nbsp;updated\_at: 2011-08-23 18:54:12.214873 &nbsp;&nbsp;id: 2 &nbsp;&nbsp;[...] -------------------
> 
> but if I do a.cost
> 
> I get error... undefined method `cost' for nil:NilClass
> 
> How is it going to be a nil class? If I do a.class I get "Part", then it's NOT a nil class.
> 
> It's weird, if I do exactly the same thing in the console a.cost does work.

Do you get an error if you do this:

\<% part = Part.find\_by\_id(the\_id) part.cost %\>

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 26, 2011, 7:49am UTC](https://discuss.rubyonrails.org/t/use-find-with-a-variable/60578/10 "2011-08-26T07:49:43Z")

</div>

> This is making me crazy, even if I do... \<%= a %\>
> 
> I get... #\<Part:0x7f2fe2bf4cf8\>
> 
> But as soon as I try to access the cost or name attributes I get... undefined method `cost' for nil:NilClass

Can you post the entirety of what's in your view?

Fred
