Fetching only the first record in the table (via the view) without changing controller?
Posted
by bgadoci
on Stack Overflow
See other posts from Stack Overflow
or by bgadoci
Published on 2010-06-07T04:10:47Z
Indexed on
2010/06/07
4:22 UTC
Read the original article
Hit count: 181
I am trying to only fetch the first record in my table for display. I am creating a site where a user can upload multiple images and attach to a post but I only want to display the first image view for each post.
For further clarification posts belong_to projects
. So when you are on the projects show page you see multiple posts. In this view I only want to display the first image for each post. Is there a way to do this in the view without affecting the controller (as later I want to allow users to browse all photos through the addition of a lightbox). Here is my /views/posts/_post.html.erb code:
<% div_for post do %>
<% post.photos.each do | photo | %>
<%= image_tag(photo.data.url(:large), :alt => '') %>
<%= photo.description %>
<% end unless post.photos.first.new_record? rescue nil %>
<%= link_to h(post.link_title), post.link %>
<%= h(post.description) %>
<%= link_to 'Manage this post', edit_post_path(post) %>
<% end %>
UPDATE: I am using a photos model to attach multiple photos to each post and using paperclip here.
© Stack Overflow or respective owner