# Limiting an :include

**URL:** <https://www.ruby-forum.com/t/limiting-an-include/108868>\
**Category:** Rails\
**Created:** [July 8, 2007, 12:21pm UTC](https://www.ruby-forum.com/t/limiting-an-include/108868 "2007-07-08T12:21:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [July 8, 2007, 12:21pm UTC](https://www.ruby-forum.com/t/limiting-an-include/108868/1 "2007-07-08T12:21:09Z")

</div>

Hello,  
I have image categories, and each category has an image. In my  
controller I’m using:

@image\_categories = ImageCategory.find(:all, :include =\> :images)

and inside the view

\<% @image\_categories.each do | category | -%\>  
\<%= category.name %\>  
\<% category.images.each do | image | -%\>  
\<%= image.name%\>  
\<% end -%\>  
\<% end -%\>

The code works, however i want to limit the display to only one image  
per category. So my question would be how can i combine :limit with the  
:include sintax ?

Thanks… any help would be really apreciated

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [July 8, 2007, 2:27pm UTC](https://www.ruby-forum.com/t/limiting-an-include/108868/2 "2007-07-08T14:27:29Z")

</div>

You might want to look into has\_one, in this case.

has\_one :main\_image, :class\_name =\> “Image”

with a :conditions or :order or something specifying how to choose that  
“main” image". It looks like you might not be using the has\_many  
relationship between Images and Categories correctly as well. I think  
you  
mean to have Category has\_many :images and Category.find(:all, :include  
=\>  
:images). ImageCategory looks a little Frankenmodel-y.

RSL
