Answer by Marc Rohloff for Display related records, if they exist, in Rails view
Another solution would be to add device_name and vendor_name methods to the DeviceModel and encapsulate that logic there.
View ArticleAnswer by Marc Rohloff for Display related records, if they exist, in Rails view
You can still create relationships on keys that are not called id (although I wouldn't recommend it. I would recommend using id, at some point someone will want to change an abbreviation and it will...
View ArticleAnswer by Adam Lassek for Display related records, if they exist, in Rails view
I can see three problems with this example.1. You are doing DB queries in the View templateA simple rule of thumb with Rails views: avoid triggering DB queries in your views at all costs.The Rails...
View ArticleAnswer by puneet18 for Display related records, if they exist, in Rails view
Using try method of ruby, exception will not be raised and nil will be returned instead, if the receiving object is a nil object. Try below code in view:# view code<% @device_vendors.each do...
View ArticleAnswer by Pavan for Display related records, if they exist, in Rails view
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...
View ArticleAnswer by Sergio Tulentsev for Display related records, if they exist, in...
You could also use first_or_initialize. It tries to find the record and, if not found, creates a new unsaved object (so that you always have an object and don't have to branch. This is called...
View ArticleAnswer by Othmane El Kesri for Display related records, if they exist, in...
There's a lot of ways to check the presence of an object, i prefer to use try :device.try(&:name)
View ArticleDisplay related records, if they exist, in Rails view
Is this much logic in view is justified to check for the presence of record?# device modelclass Device < ApplicationRecord validates :abbr, uniqueness: { case_sensitive: false }, presence: true...
View Article