mediarts
mediarts

Reputation: 1521

Rails 3: Only show the first image from the Database

I'm using rails 3.

In my database I have several tables that work together using one common column "property_id" to relate them. In my property_images table, there are several images with the same property_id. I use the following code to show all the images in the property/show page.

<% for image in @property.property_images %>
    <%= image_tag image.image_url %>
<%end%>

I'd like to be able to display only the first image for the property_id in another area on that page. Does anyone know how I'd write the logic for that in the controller and place it in the property/show page?

Upvotes: 0

Views: 655

Answers (2)

asdfpoiuyt
asdfpoiuyt

Reputation: 1

<% for image in @property.property_images.limit(1) %>
<%= image_tag image.image_url %>

<%end%>

Upvotes: 0

Doon
Doon

Reputation: 20232

you could do something like this.

<%= image_tag @property.property_images.first.image_url %> 

Since property_images is just an array...

Upvotes: 1

Related Questions