You can try this way:
@item.category.try(:description)
here description is the column name in your category table
题
I have the following code in my Show View for a Item model
That is a:
has_one :categorie
View:
<hr>
<label>Categorie:</label> <%= @item.categorie_id %> </br>
<label>Name: </label> <%= @item.nome_ptbr || @item.nome_en %> </br>
<label>Desc: </label> <%= @item.desc %> </br>
<hr>
But instead of showing like 'Categorie: 3', I want to show 'Categorie: Band'.I got this code working* but I don't know if there is a better and right way to do it.
<hr>
<label>Categorie:</label> <%= Categorie.find_by(id: @item.categorie_id).description %> </br>
<label>Name: </label> <%= @item.nome_ptbr || @item.nome_en %> </br>
<label>Desc: </label> <%= @item.desc %> </br>
<hr>
Thanks!
解决方案
You can try this way:
@item.category.try(:description)
here description is the column name in your category table