Quantcast
Channel: Display related records, if they exist, in Rails view - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 8

Answer by Pavan for Display related records, if they exist, in Rails view

$
0
0

Is this much logic in view is justified to check for the presence of record?

It is justified to check for a presence of a record in the view but you are writing AR queries in the view which will trigger DB calls and its not the Rails way to do it! You should instead move those to corresponding view helper or define it as model methods. I would do

#view_helperdef get_device_name_and_vendor_name(dv)  device = Device.where(abbr: dv.device).first  device_name = device.blank? ? nil : device.name  vendor = Vendor.where(abbr: dv.vendor).first  vendor_name = vendor.blank? ? nil : vendor.name  return device_name,vendor_nameend

And in the view, call that method to get device name and vendor name

<td><%= get_device_name_and_vendor_name(device_vendor)[0] %></td><td><%= get_device_name_and_vendor_name(device_vendor)[1] %></td>

Note:I would also instead apply associations to the models to cleanup those queries.


Viewing all articles
Browse latest Browse all 8

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>