【发布时间】:2017-03-22 22:01:50
【问题描述】:
这是场景:
所以,产品与反馈没有任何关系,但订单有 我想将反馈显示为属于产品
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
我想循环获得产品的反馈
例如
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>
我得到了这个代码
nil:NilClass 的未定义方法 `id'
有人有提示吗?谢谢
【问题讨论】:
-
欢迎来到 Stack Overflow。 SO 就像一个在线参考网站,语法和拼写都很重要。请在发帖前使用语法检查器和/或拼写检查器。
标签: mysql ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4