mercredi 22 mars 2017

Rails how iterate has_one though?

This is the scenario:

so, the product did not have any relation with feedback, but the order has and i would like to show the feedbacks as belongs to products

model FEEDBACK

 has_one :order
 has_one :product, through: :order 

model ORDER

  belongs_to :product, touch: true
  belongs_to :feedback

model PRODUCT


  has_many :feedbacks
  has_many :orders

i'm would like to get the feedbacks of the product on a loop

for exemple

controller HOME


    @total_feedbacks = Feedback.joins(:product).where('products.id = ?', @product.id).where('buyer_feedback_date is not null').count


    @average_rating_from_buyers = Feedback.joins(:product).where('products.id = ?', @product.id).where('buyer_feedback_date is not null').rated(Feedback::FROM_BUYERS).average(:buyer_rating)






view HOME



  <table id="posts" class="product_table">
        <tbody class="page">
          <tr>
            <% @products.last(22).each do |product|%>

              <td>
                <%=link_to product_path(product.id), :class=>"product" do %>

                  <span class="productName">
                    <%=product.name %></span>
                  <span class="price"><%=number_to_currency(product.price, :unit=> "R$") %></span>


              <% if product.vitrine.feedbacks.present? %>
                    <div class="productFeeback">
                      <div><%= @average_rating_from_buyers %>"></div>
                      <%=@total_feedbacks %>
                    </div>
                  <% end %>

                <% end %>
              </td>
            <% end %>
          </tr>



        </tbody>
      </table>

with this code i'm getting

undefined method `id' for nil:NilClass

someone have a hint to spare? thank's

Aucun commentaire:

Enregistrer un commentaire