【问题标题】:How to display related items如何显示相关项目
【发布时间】:2017-08-23 09:03:50
【问题描述】:

我想在本地体验下方显示相关体验。我尝试使用@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>

【问题讨论】:

  • 什么是排除?你说的没有解决是什么意思?
  • 但没有解决错误是什么?
  • excludes 只是 Include 方法的反向是 rails 并且您似乎想要排除每个房子的经历 ryt?我想知道在 DB(has_many 或 has_one)中房子和经验是如何相关的?
  • @VKatz 有没有方法叫excludesexclude?
  • house has_many :experiences @VKatz

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


【解决方案1】:

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

对于您的问题,鉴于house_id 是您的经验表中的foreign_key,也许类似下面的内容会起作用。

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

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

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 2012-08-22
    • 2021-06-13
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-01
    • 2012-09-25
    相关资源
    最近更新 更多