Q:

How to retrieve a value of a "belongs to" element attribute on an index page in rails?

I try to make a web application where I have 4 models :
User, Recipe, Step, Steps_advancement.
Globally, a user can like a recipe composed by steps. When the user opens and reads a step of the recipe, it puts a "is_read=true" on the table steps_advancement which belongs to user and step.
For the moment I have this code :
Models :
class Recipe < ApplicationRecord
belongs_to :user
has_many :steps
has_many :favorites
has_many :favorited_by_users, :through => :favorites, :source => 'user'

class Step < ApplicationRecord
belongs_to :recipe
has_one :steps_advancement
end

class StepsAdvancement < ApplicationRecord
belongs_to :step 
belongs_to :user
end

class Favorite < ApplicationRecord
belongs_to :recipe
belongs_to :user
end

class User < ApplicationRecord
has_many :recipes
has_many :favorites
has_many :favorite_recipes, :through => :favorites, :source => 'recipe’
has_many :steps_advancements

devise :database_authenticatable, :registerable,
 :recoverable, :rememberable, :trackable, :validatable
end
end

In the database, i have a table Steps_advancement with a boolean "is_read", default: false, null: false (and recipe_id and step_id on index)
In the views, the steps index page lists all the steps of the recipe.
I want to be able to retrieve, in the steps index view, the value of the steps_advancement ‘is_read’. In order to display it if the previous is read, or hide it, else. 
But I can't, I have an error message.
StepsController
def index
@recipe = Recipe.find(params[:recipe_id])
@steps = @recipe.steps

in the steps index view (index.html.erb)
<% if current_user %>        
<% @steps.each do |st|%>                    
<% if st.steps_advancement.is_read?%>   # <<< error here        
<%= link_to([@recipe, steps]) do %>

<strong><%=st.name %></strong> --             
<%else%>

<%="not avalaible for the moment!"%>

<% end %>       
<% end %>    

In the third line I have a problem. I cannot access the steps_advancement. I guess rails does not recognize the step but how can i resolve it ? :)
In advance thanks!

A:

So step_advancement is not created on step until it is read, correct?
Add a try before is_read? like such:
steps/index.html.erb
<% if current_user %>        
<% @steps.each do |st|%>                    
<% if st.steps_advancement.try(:is_read?)%>   # <<< error here        
<%= link_to([@recipe, steps]) do %>

