List elements of the has_many: through relationship

2

I'm creating an application and have a has_many: through relationship in my model. So far so good, it worked without problems, saves, edits and deletes. The problem is that I would like to list all the elements that are in the third table of this relation in the view. I've been looking for hours and trying to do more without success. Home This is my template:

class Imovel < ApplicationRecord
  has_many :caracteristica_imovels, inverse_of: :imovel
  has_many :caracteristicas, through: :caracteristica_imovels, dependent: :destroy
  accepts_nested_attributes_for :caracteristica_imovels, reject_if: :all_blank, allow_destroy: true
end

In the view I'm trying to print all the features of the property, but to no avail. I tried this way:

<% @imovel.caracteristicas.each do %>
      <%= @imovel.caracteristicas.nome %>
  <%end%>

From this too:

<%= @imovel = Imovel.joins(:caracteristica_imovel).includes(:nome)%>

Of this second form errors do not happen but only gets printed: #<Imovel::ActiveRecord_Relation:0x007fe915b469e0>

How can I make this listing?

    
asked by anonymous 27.05.2017 / 20:25

1 answer

1

try this way:

<% @imovel.caracteristicas.each do |item| %>
   <dd><%= item.nome %> </dd> 
<% end %>
    
27.05.2017 / 20:48