【问题标题】:Query "where" with ruby on rails使用 ruby​​ on rails 查询“where”
【发布时间】:2015-05-19 08:27:29
【问题描述】:

我在我的应用中以创意的形式发布,这些创意属于一个活动和一个状态。 我想按一个状态的活动对它们进行排序,所以我在我的控制器中做了

@idees_en_place = Idee.where(statut_id= "2")
@activites = Activite.all

在我看来:

<% @activites.each do |activite| %>
  <div class="idee en-place col-lg-5" style="background:#<%= activite.color%>">
    <h2><%= activite.name %></h2>
    <p>
     <% @idees_en_place.where(activite_id = activite.id).limit(3).each do |idee| %>
     <div class="idee">
       <h6><%= link_to idee.title, idee %></h6>
      </div>
     <% end %>
  </p>
  </div>
<% end %>

但这不起作用,在活动的每个部分中,想法都没有排序。 我认为这是一个小错误,但我不知道如何解决这个问题

【问题讨论】:

  • 应该是:@idees_en_place = Idee.where(statut_id: 2)
  • 你能补充一下你是如何映射你的模型的吗?

标签: ruby-on-rails ruby where


【解决方案1】:
@idees_en_place = Idee.where(statut_id= "2")

这段代码有两个问题。

首先,id 是一个Integer 类型(除非您将其定义为String)。 其次,它是您传递给where 子句的键值,您可以将它们传递为

:status_id => 2 # old hashrocket syntax

status_id: 2 # new syntax

这部分也是如此

@idees_en_place.where(activite_id = activite.id)

应该是

@idees_en_place.where(activite_id: activite.id)

【讨论】:

  • 谢谢!但现在,没有显示任何想法
  • @MilletoSimon 这是您的问题以及您的应用程序中模型之间的关联,而不是我:) 很可能没有Ideas 和status_id2。如果它教会了您有关查询数据库的知识,请接受答案
【解决方案2】:

控制器

@idees_en_place = Idee.where(statut_id: 2)
@activites = Activite.all

查看

<% @activites.each do |activite| %>
  <div class="idee en-place col-lg-5" style="background:#<%= activite.color%>">
    <h2><%= activite.name %></h2>
    <p>
     <% @idees_en_place.where(activite_id: activite.id).limit(3).each do |idee| %>
     <div class="idee">
       <h6><%= link_to idee.title, idee %></h6>
      </div>
     <% end %>
  </p>
  </div>
<% end %>

【讨论】:

  • 不应该永远在视图中执行查询。在 100 个活动中,将执行 100 个针对数据库的查询。
【解决方案3】:

我只想指出,您将遇到 N+1 查询问题,为避免这种情况,您应该预加载所有内容,而不是在视图中进行查询。

控制器:

#change if the association name is different
@activites = Activite.includes(:idees)

观点

<% @activites.each do |activite| %>
  <div class="idee en-place col-lg-5" style="background:#<%= activite.color%>">
    <h2><%= activite.name %></h2>
    <p>
     <% activitie.idees[0..2].each do |idee| %>
     <div class="idee">
       <h6><%= link_to idee.title, idee %></h6>
      </div>
     <% end %>
  </p>
  </div>
<% end %>

注意事项:

我使用了[0..2] 格式,因为我想避免 ActiveRecord 执行新查询,另一种方法是使用类似这样的方式限制查询

@activites = Activite.includes(:idees).merge(Idee.limit(3))

那么您将不需要在视图中使用任何限制,但我还没有测试过这个,现在没有在 rails 机器上的访问权限。

【讨论】:

    【解决方案4】:

    我认为下面的代码会对你有所帮助:

    由于您的 Idee 属于活动和状态,因此您的 Idee 表中有 activity_id 和 status_id。

    您可以通过以下方式找出状态的所有想法:

    Idee.where(:status_id => 2)
    

    您可以使用 order 以 Asc 或 desc 顺序对 Idee 进行排序

    idees = Idee.join(:activity).where(:status_id => 2).order(:activity_id => :asc)
    idees = Idee.join(:activity).where(:status_id => 2).order(:activity_id => :desc)
    
    <% activity_id = -1%>
    <@idees.each do |idee| %>
      <div class="idee en-place col-lg-5" style="background:#<%= idee.activite.color%>">
      <h2><%= idee.activity.name %></h2>
      <p>
        <% if activity_id != idee.activity_id  %>
          <% activity_id = idee.activity.id %>
          <% counter = 0 %>
        <% end %>
        <% if counter < 3 %>
          <% counter = counter + 1%>
          <div class="idee">
            <h6><%= link_to idee.title, idee %></h6>
          </div>
        <% end %>
      </p>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2012-12-16
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多