List has_many Relation Elements :through

Asked

Viewed 152 times

2

I am creating an application and realized a has_many :through relation in my template. So far so good, worked smoothly, saved, 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 am hours looking and trying to do more without success.
This is my model:

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 am trying to print all the characteristics of the property, however, without success. I tried this way:

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

This one too:

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

This second way mistakes do not happen but just gets printed: #<Imovel::ActiveRecord_Relation:0x007fe915b469e0>

How can I make this listing?

1 answer

1


tries this way:

<% @imovel.caracteristicas.each do |item| %>
   <dd><%= item.nome %> </dd> 
<% end %>
  • You saved the day my friend. So: <% @immovel.caracteristicas.each do |item| %> <dd><%= item.name %> </dd> <% end %> worked! Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.