Quantcast
Channel: Display related records, if they exist, in Rails view - Code Review Stack Exchange
Browsing latest articles
Browse All 8 View Live

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 Article



Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Display 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

Browsing latest articles
Browse All 8 View Live




Latest Images