Rails 2.3.8.
I have this in my controller where I limit only 1 result:
@photos = @shop.photos.find(:all, :limit => 1)
In my view, usually I just do a for loop to display the result:
<% @photos.each do |photo| %>
<%= image_tag(photo.data.url(:preview)) %>
<% end %>
Now the above is for multiple values in an array. If I only have 1 value, must I continue using this method? Thanks.
You don’t need to store the result in an array. The first parameter of
findis whether you want to select:allrecord or just the:first. You can store the only the first result in a variable@photo:Then you can just display this one photo without looping:
Hope this helps!