Médoto 1: Accessing the desired field in each record
If in your controller you are assigning @f
all the records you want to display
#/app/controllers/terminals_controller.rb
@f = Terminal.all
Then to show a single field, in this case name, you must iterate over all the records that the variable @f
has with the method .each
and for each record show the field of interest:
#/app/views/terminals/index.html.erb
<% @f.each do |f| %>
<%= f.nombre %><br>
<% end %>
Medoto 2: Using pluck
In Rails 3.2 or higher, another way would be using .pluck
, which would only load the column that you indicate
#/app/controllers/terminals_controller.rb
@f2 = Human.pluck(:name)
And to show in the view it would be something like this:
#/app/views/terminals/index.html.erb
<% @f2.each do |f| %>
<%= f %><br>
<% end %>
Here is the link for the documentation of pluck .