Forum: Ruby on Rails Limiting an :include

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daniel M. (Guest)
on 2007-07-08 14:21
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
Russell N. (Guest)
on 2007-07-08 16: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
you
mean to have Category has_many :images and Category.find(:all, :include
=>
:images). ImageCategory looks a little Frankenmodel-y.

RSL
This topic is locked and can not be replied to.