I have a model that has a main_image per asset and in the attachment model this can only have one true value per asset. I am wondering if there is a way to pull this record without looping through every record to see if main_image is set to true or false.
Clarification:
I can find the value by using the following code:
<% @asset.attachments.each_with_index do |attachment, i| %>
<%= image_tag(attachment.attachment.s_640_480) if !attachment.main_image.nil? && attachment.main_image%>
<%end%>
but, I am wondering how to do this without a loop…
I don’t know how to clarify any more…but something like:
<%= image_tag(@attachment.where_main_image.s_640_480) %>
I know that won’t work but basically that is the concept
It’s not so nice to have this code in your view, so I’d recommend to put it in an instance method of your asset model:
Then in your view you can do:
You probably have to add some checks that objects are not nil. Good luck.