In my Rails app there are experiment
instances and each experiment has attachments
, which are represented as binary blobs in the database and can be quite big. This question is about efficiency in coding a link_to show the attachment.
Originally I had this:
<%= link_to @experiment.attachment.file_name, @experiment.attachment %>
However I was told that the Rails app would be more efficient in rendering the page with
<%= link_to @experiment.attachment.file_name, {:controller => :attachments, :action => :show, :id => @experiment.attachment_id}, {:method => :get} if ! @experiment.attachment_id.nil? %>
The justification is that the first version fetches the attachment from the database, and the second one does not, making it is better, albeit longer and uglier. Is this true?
Both versions accomplish the same thing in directing the user to the show page for an attachment and I was under the impression the first is the default way to do a link_to a record show page.
Is there a way to shorthand the second piece of code to make it less terrible with code in the view?