如何显示相关项目

时间:2017-08-23 09:03:51

标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-5

我想在当地经历下展示相关经验。我尝试使用@experiences= Experience.excludes(house.experiences)来做,但没有成功。我可以得到任何建议如何做到这一点。

<h3>Local experiences</h3>

<section class="list-box">
  <% @house.experiences.each do |experience| %>

  <p>
    <%= experience.name %>
    <% if logged_in? && current_user.role == "customer" %>
    <%= link_to "Make Enquiry", new_message_path(receiver_id: experience.supplier.user) %>
    <% else %>
    <%= link_to "Make Enquiry", new_customer_path %>
    <% end %>
  </p>
  <% end %>
</section>

<h3>Related experiences</h3>

<section class="list-box">
  <% @experiences.each do |experience| %>

  <p>
    <%= experience.name %>
    <% if logged_in? && current_user.role == "customer" %>
    <%= link_to "Make Enquiry", new_message_path(receiver_id: experience.supplier.user) %>
    <% else %>
    <%= link_to "Make Enquiry", new_customer_path %>
    <% end %>
  </p>
  <% end %>
</section>

1 个答案:

答案 0 :(得分:0)

rails模型中没有名为excludes的方法。只有exclude?,它是类String的实例方法。请参阅here

对于您的问题,考虑到house_id是您体验表中的foreign_key,下面的内容可能会有效。

@experiences = Experience.where.not(house_id: house.experiences)

您可以参考here了解更多not条件。