Forum: Ruby on Rails Limiting an :include

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
7515cb5c8df2d07f6d3780346ec0da92?d=identicon&s=25 Daniel Mircea (viseztrance)
on 2007-07-08 12:21
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.images.each do | image | -%>
    <% 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
5030981121b21bed8aee074f68bd5074?d=identicon&s=25 Russell Norris (Guest)
on 2007-07-08 14:27
(Received via mailing list)
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
mean to have Category has_many :images and Category.find(:all, :include
:images). ImageCategory looks a little Frankenmodel-y.

This topic is locked and can not be replied to.