# Hmmmm..sounds so simple but

**URL:** <https://www.ruby-forum.com/t/hmmmm-sounds-so-simple-but/133789>\
**Category:** Rails\
**Created:** [April 3, 2008, 4:53pm UTC](https://www.ruby-forum.com/t/hmmmm-sounds-so-simple-but/133789 "2008-04-03T16:53:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![teenage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@teenage](https://www.ruby-forum.com/u/teenage)\
**Post date:** [April 3, 2008, 4:53pm UTC](https://www.ruby-forum.com/t/hmmmm-sounds-so-simple-but/133789/1 "2008-04-03T16:53:20Z")

</div>

Hey,  
I’m having a little problem…  
I’m trying to display a list of say “entries”  
Each entry has\_many images but in the main listing I want to only  
display 1 image against each post… how do I do that!!! Sounds so simple  
but can’t work it out…

here’s the code:

CONTROLLER

def index  
@users = User.find(:all, :select =\>“screen\_name, id”)  
@projects = Project.find(:all, :select =\>“id, title, permalink,  
category\_id, user\_id”)  
end

VIEW

\<% for project in @projects %\>  
\<% for image in project.images %\>  
\<%= link\_to(image\_tag(image.public\_filename(:mini)), :action =\>  
project.user.screen\_name, :id =\> project) %\>  
\<% end %\>

\<%= link\_to h(project.title), :action =\> project.user.screen\_name, :id  
=\> project %\>\<%= project.category.category\_name %\>  
\<% end %\>

So If I have only one image per project, this is fine but (obviously) if  
I have multiple images on one project, they all come up because of the  
for loop…

Please please please someone help, I’m going mad :S

thanks  
Oli

---

<div class="post-metadata">

**Author:** ![teenage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@teenage](https://www.ruby-forum.com/u/teenage)\
**Post date:** [April 3, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/hmmmm-sounds-so-simple-but/133789/2 "2008-04-03T16:57:26Z")

</div>

kick out the loop and use project.images.first instead

---

<div class="post-metadata">

**Author:** ![teenage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@teenage](https://www.ruby-forum.com/u/teenage)\
**Post date:** [April 3, 2008, 5:05pm UTC](https://www.ruby-forum.com/t/hmmmm-sounds-so-simple-but/133789/3 "2008-04-03T17:05:24Z")

</div>

On 03 Apr 2008, at 16:53, Olivier B. wrote:

> \<% for project in @projects %\>  
> So If I have only one image per project, this is fine but  
> (obviously) if  
> I have multiple images on one project, they all come up because of the  
> for loop…
> 
> Please please please someone help, I’m going mad :S

Although you could use Thorsten’s images.first technique, you’re  
loading all images while you only need one. The method below could be  
more performant and you can change the main\_image logic if necessary  
in your model, where it should belong.

MODEL

class Project  
has\_one :main\_image, :class\_name =\> “Image”, :foreign\_key =\>  
“project\_id”  
# leave all the rest you have here too, such as the one-to-many  
relationship, for when you need all the images  
…  
end

CONTROLLER

def index  
@users = User.find(:all, :select =\>“screen\_name, id”)  
# Eager load the main\_image, it’s going to be more performant and  
just lose the select  
@projects = Project.find(:all, :include =\> [:main\_image])  
end

VIEW

\<% for project in @projects %\>  
\<%=  
link\_to(image\_tag(project.main\_image.public\_filename(:mini)), :action =\>  
project.user.screen\_name, :id =\> project) %\>

\<%= link\_to h(project.title), :action =\> project.user.screen\_name, :id  
=\> project %\>\<%= project.category.category\_name %\>  
\<% end %\>

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![teenage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@teenage](https://www.ruby-forum.com/u/teenage)\
**Post date:** [April 3, 2008, 5:19pm UTC](https://www.ruby-forum.com/t/hmmmm-sounds-so-simple-but/133789/4 "2008-04-03T17:19:57Z")

</div>

Peter

works a treat… thank you ever so much for your help… It’s my first  
time here and I ma very VERY impressed.

Thanks again  
Olivier
