I am using paperclip to upload multiple images to a model, I have a post model which has_many photos.
When I am displaying these in the view and there are more than one image assigned to the post the post gets multiplied by the amount of photos, so if there are three images i get three identical posts.I know why this is happening as I am looping through the records like so
<ul>
<% @tynewyddpost.each do |t| %>
<li>
<% t.photos.each do |p| %>
<a class="photo" href="#"><%= image_tag p.avatar.url(:thumbnail_news_images) %></a>
<p><a href=""><%= t.title %></a></p>
<p class="date"><%= t.created_at %></p>
</li>
<% end %>
<% end %>
</ul>
How do i state that i only want one displayed if there are more than one, which method do i need to look at?
Thanks
Ok so found the answer
<ul>
<% @tynewyddpost.each do |t| %>
<li>
<% single = t.photos.first %>#Added this above call for image
<a class="photo" href="#"><%= image_tag(single.avatar.url(:thumbnail_news_images)) %></a>#call first image like this
<p><a href=""><%= t.title %></a></p>
<p class="date"><%= t.created_at %></p>
</li>
<% end %>
</ul>