greyoxide
greyoxide

Reputation: 1287

show first record in array on index.html.erb

I have an Image model that is linked by a many-to-one relationship with the Post model.

On the index action I would like to display only the first image. @post.images.each works well but @post.image.first does not. So what I am wondering is: while in the view how does one display only the first record from the images array?

Upvotes: 0

Views: 1398

Answers (1)

Benjamin Bouchet
Benjamin Bouchet

Reputation: 13181

As you have a many-to-one relationship:

  • @post.images (with a 's') is a collection of images
  • @post.image (without 's') does not exist

So to get the first image you must use: @post.images.first (with a 's')

Upvotes: 2

Related Questions