# Has\_one and find\_by\_id issue

**URL:** <https://www.ruby-forum.com/t/has-one-and-find-by-id-issue/137780>\
**Category:** Rails\
**Created:** [May 16, 2008, 9:56am UTC](https://www.ruby-forum.com/t/has-one-and-find-by-id-issue/137780 "2008-05-16T09:56:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![heimdull](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f4b2a3/32.png) [@heimdull](https://www.ruby-forum.com/u/heimdull)\
**Post date:** [May 16, 2008, 9:56am UTC](https://www.ruby-forum.com/t/has-one-and-find-by-id-issue/137780/1 "2008-05-16T09:56:51Z")

</div>

I have two models:

testamonial  
photo (has\_attachement)

testamonial has\_one photo

The issue is when do this:  
@testamonial = Testamonial.find\_all\_by\_id(id)

I get “undefined method `photo’ for” in my browser?? BUT here is the  
big kicker…

If I do @testamonial = Testamonial.find(:first)

everything works fine and the image is show!.. (find :all works too  
when I loop through the objects.)

I tried @testamonial = Testamonial.find\_all\_by\_id(id, :include  
=\> :photo) That just gave me a bigger query and still no photo…

---

<div class="post-metadata">

**Author:** ![heimdull](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f4b2a3/32.png) [@heimdull](https://www.ruby-forum.com/u/heimdull)\
**Post date:** [May 16, 2008, 11:31am UTC](https://www.ruby-forum.com/t/has-one-and-find-by-id-issue/137780/2 "2008-05-16T11:31:48Z")

</div>

On 16 May 2008, at 08:56, heimdull wrote:

> I get “undefined method `photo’ for” in my browser?? BUT here is the  
> big kicker…

That returns an array of testamonials, not a single object as  
find :first does.

Fred

---

<div class="post-metadata">

**Author:** ![heimdull](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f4b2a3/32.png) [@heimdull](https://www.ruby-forum.com/u/heimdull)\
**Post date:** [May 16, 2008, 11:28am UTC](https://www.ruby-forum.com/t/has-one-and-find-by-id-issue/137780/3 "2008-05-16T11:28:36Z")

</div>

heimdull wrote:

> If I do @testamonial = Testamonial.find(:first)
> 
> everything works fine and the image is show!.. (find :all works too  
> when I loop through the objects.)

Are you sure the id matches the first record in the table?

---

<div class="post-metadata">

**Author:** ![heimdull](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f4b2a3/32.png) [@heimdull](https://www.ruby-forum.com/u/heimdull)\
**Post date:** [May 16, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/has-one-and-find-by-id-issue/137780/4 "2008-05-16T18:20:49Z")

</div>

And thats why I say never post to a forum in the middle of the night :  
(

duh…

Changed the view code and its all good now…

Thanks Fred
