-1

I have a Products model with a 'has_many' relationship to Microposts. Likewise, Microposts has a 'belongs_to' relationship to Products. throught this relationship I want to populate a row in table with all the available microposts for that product. From what I can tell the association works fine and the rows have potential to display the content, however "#Micropost::ActiveRecord_Associations_CollectionProxy:0x007fa5ba8b4d60" is displayed instead of the actual content.

Micropost.rb

class Micropost < ActiveRecord::Base
  belongs_to :product
  default_scope -> { order(created_at: :desc) }
  validates :product_id, presence: true
  validates :content, presence: true
end

Product.rb

class Product < ActiveRecord::Base
before_save{
self.name = name.downcase.humanize}
before_save{
self.oem = oem.downcase.humanize}

validates :ta_code,  presence: true, length: { maximum: 4, minimum: 3 }
validates :tatr,  presence: true, length: { maximum: 4, minimum: 4 },       uniqueness: true
validates :name,  presence: true
validates :oem,  presence: true
validates :kind,  presence: true
validates :ta_type,  presence: true
has_many :microposts
end

code

<% Product.all.each do |product| %>
.
.
.
<% if product.microposts.any? %>
            <ul class="microposts">
              <% product.microposts.each do |micropost| %>
              <li><%= micropost.content %></li>
              <% end %>
              </ul>
            <%= link_to "Update status", new_status_path(product_id: product.id), :method => :create %>
            <% else %>
            <%= link_to "Update status", new_status_path(product_id: product.id), :method => :create %>
            <% end %>

1 Answers1

0

You could simply change the display part to the following

<% Product.all.each do |product| %>
.
.
.
  <ul class="microposts">
    <%product.microposts.each do |micropost| %>
      <li><%=micropost.content%></li>
    <% end %>
  </ul>
Ojash
  • 1,234
  • 8
  • 20