This problem is very common, but I can not solve it in any way. This is about ruby ​​on rails.

class Product < ApplicationRecord belongs_to :product_shop, optional: true end class ProductShop < ApplicationRecord has_many :products end 

the compiler complains about this line:

  <td><%= product.product_shop.name %></td> 

Sorry for the banal question, but what could be the error, I just can not figure it out ...

  • That product.product_shop not product.product_shop (equal to nil ). - D-side

1 answer 1

You can add before

 <td><%= product.product_shop.name %></td> 

checking for "nil?"

 unless product.product_shop.nil? <td><%= product.product_shop.name %></td> 
  • It is even fun . - D-side
  • @ D-side, apparently it did solve the problem? Answer accepted .. - vp_arth
  • one
    @vp_arth especially considering that the added code will not be executed, but simply displayed, in plain text. I rather believe that the vehicle has received a reminder from the system that it is necessary to accept the answer, and he (a?) Accepted (a?) The one that was. - D-side