mysql - Ruby 2 & Rails 4 查询在开发中返回结果,但在生产中返回 nil

标签 mysql ruby ruby-on-rails-4

我们正在用 ruby​​ on rails 编写一个应用程序,但遇到了一个奇怪的问题。 当我们在开发中运行应用程序时,我们的查询返回得很好。 当我们在生产环境中运行该应用程序时,即使数据库中有记录,我们的查询也会返回 nil。我直接通过 MySQL 服务器查询,所以我知道记录在那里。

有什么想法吗?我已经包含了错误、迁移、模型、 Controller 和 View 代码。

我得到的错误是

undefined method `each' for nil:NilClass

提取的源代码(大约第 19 行):

          @count = 1
          if @leadsHot != nil || @leadsHot != ''
          @leadsHot.each do |lead| %>
          <tr role="row" <% if @count / @count == 0 %>class="gradeA even"<% else %> class="gradeA odd" <% end %>>
          <td class="sorting_1"><%= lead.category.to_s %></td>
          <td><%= lead.fname.to_s %> <%= lead.l_name.to_s %></td>

迁移文件

  class CreateLeads < ActiveRecord::Migration
  def up
    create_table :leads do |t|
      t.column :category, :string
      t.column :patent_status, :string
      t.column :over_eighteen, :string
      t.column :salute, :string
      t.column :fname, :string
      t.column :m_initial, :string
      t.column :l_name, :string
      t.column :address, :string
      t.column :address1, :string
      t.column :city, :string
      t.column :state, :string
      t.column :zip,:string
      t.column :phone, :string
      t.column :cell_phone, :string
      t.column :fax, :string
      t.column :email, :string
      t.column :user_id, :integer
      t.column :source, :string
      t.column :lead_source_id, :integer
      t.column :hot, :boolean, :null => false, :default => 0
      t.column :comments, :text
      t.column :active, :boolean
      t.timestamps null: false
    end
  end

  def down
    drop_table :leads
  end
end

模型

class Lead < ActiveRecord::Base
    has_one :lead_source
    accepts_nested_attributes_for :lead_source

    has_many :lead_contact_logs
  end

Controller

class Backoffice::SalesController < ApplicationController
  include LogIt
  include LeadManagement
  include SalesModule
  include TableDataMethods
  def dashboard
    @leadsAll = Lead.all().where("hot=0 AND active=1") #getvalidleads
    @leadsHot = Lead.all().where("hot=1 AND active=1")  #getHotLeads
    @leadsDOA = Lead.all().where("active=0") #getRecentDeadLeads
    @curClients = basicCurrentClientsTable
    if @curClients == nil
      @curClients = "<tr><td colspan='7'>No Clients Available</td></tr>"
    end
    if @leadsHot == nil
      @leadsHot = "<tr><td colspan='7'>No Hot Leads Available</td></tr>"
    end
  end
end

查看

<div class="panel-body" style="display: none;">

    <table aria-describedby="data-table_info" role="grid" class="table table-striped table-bordered nowrap dataTable no-footer dtr-inline" id="hotLeads">
      <thead>
      <tr role="row">
        <th aria-label="Lead Category: activate to sort column descending" aria-sort="ascending" colspan="1" rowspan="1" aria-controls="data-table" tabindex="0" class="sorting_asc">
          Lead Category
        </th>
        <th aria-label="Contact Name: activate to sort column ascending" style="" colspan="1" rowspan="1" aria-controls="data-table" tabindex="0" class="sorting">Contact Name</th>
        <th aria-label="Phone: activate to sort column ascending" style="" colspan="1" rowspan="1" aria-controls="data-table" tabindex="0" class="sorting">Phone</th>
        <th aria-label="Email: activate to sort column ascending" style="" colspan="1" rowspan="1" aria-controls="data-table" tabindex="0" class="sorting">Email
        </th>

        <th aria-label="ACTIONS: activate to sort column ascending" style="" colspan="1" rowspan="1" aria-controls="data-table" tabindex="0" class="sorting">
          ACTIONS
        </th>
      </tr>
      </thead>
    <tbody>
    <%
          @count = 1
       if @leadsHot != nil || @leadsHot != ''
       @leadsHot.each do |lead| %>
        <tr role="row" <% if @count / @count == 0 %>class="gradeA even"<% else %> class="gradeA odd" <% end %>>
          <td class="sorting_1"><%= lead.category.to_s %></td>
          <td><%= lead.fname.to_s %> <%= lead.l_name.to_s %></td>
          <td><%= lead.phone.to_s %></td>
          <td><%= lead.email.to_s %></td>
          <td>
            <div class="btn-group m-r-5 m-b-5">
              <a href="javascript:;" class="btn btn-success">Actions</a>
              <a href="javascript:;" data-toggle="dropdown" class="btn btn-success dropdown-toggle">
                <span class="caret"></span>
              </a>
              <ul class="dropdown-menu pull-right">
                <li><a href=/backoffice/sales/view_lead?id=<%= lead.id %>">View</a></li>
                <li><a href="/backoffice/sales/edit_lead?id=<%= lead.id %>">Edit</a></li>
                <li><a href="mailto:<%= lead.email %>">Email - <%= lead.email %></a></li>
                <li><a href="/backoffice/process/new_client/create_client?id=<%= lead.id %>">Convert To NEW CLIENT</a></li>
                <li><a href="/backoffice/sales/edit_lead?id=<%= lead.id %>&update=makeDead">Convert To DEAD LEAD</a></li>
              </ul>
            </div>
          </td>
        </tr>
    <% end %>
    <%
       else
    %><tr><td colspan="7"><h5>No Hot Leads in the system.</h5></td></tr><%
       end
    %>
    </tbody>
  </table>
</div>

rails 控制台生产结果

   2.2.1 :004 > Lead.all().where(active: true, hot: true)
  Lead Load (0.8ms)  SELECT `leads`.* FROM `leads` WHERE `leads`.`active` = 1 AND `leads`.`hot` = 1
 => #<ActiveRecord::Relation [#<Lead id: 1, category: "Cars", patent_status: "none", over_eighteen: "Yes", salute: "Mr", fname: "Middleton", m_initial: "Jacksonmade", l_name: "Longtrowsers", address: "303 Springfield Ave", address1: "Apt D", city: "Lancaster", state: "North Carolina", zip: "97055", phone: "(803) 320-7672", cell_phone: "803-320-7672", fax: "", email: "george@betazedsoftwaresolutionsinc.com", user_id: nil, source: "Call In", lead_source_id: nil, hot: true, comments: "sdgsadfasdg", active: true, created_at: "2015-12-10 08:45:59", updated_at: "2015-12-10 08:45:59">, #<Lead id: 2, category: "Cars", patent_status: "Yes", over_eighteen: "Yes", salute: "Mr.", fname: "Furgo", m_initial: "Robert", l_name: "Man", address: "303 Springfield Ave", address1: "Apt D", city: "Lancaster", state: "North Carolina", zip: "97055", phone: "(803) 320-7672", cell_phone: "803-320-7672", fax: "", email: "billing@betazedsoftwaresolutionsinc.com", user_id: nil, source: "Call In", lead_source_id: nil, hot: true, comments: "asdfasdfasdf", active: true, created_at: "2015-12-10 08:52:21", updated_at: "2015-12-10 08:52:21">]> 

最佳答案

像这样的查询 where("hot=0 AND active=1") 依赖于特定的数据库,因为并非所有数据库都编码 truefalse 作为 10

这样写你的查询:

Lead.where(hot: false, active: true)

关于mysql - Ruby 2 & Rails 4 查询在开发中返回结果,但在生产中返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34205348/

相关文章:

ruby-on-rails - 如何将图像链接到(可点击)页面

ruby-on-rails-4 - rails 4 中的自定义日期时间格式

mysql触发器 - 如何在插入后添加记录ID

mysql - 无法理解 SQL 中的 CASE WHEN 语句

php - 这个系统有什么更好的 solr 查询?

c# - 使用 XML-RPC 编写以 C#、Ruby 和 Java 编写的分布式应用程序

MySQL显示多个表中的列

ruby - 在两个 ActiveRecord 类之间合并/复制属性的好方法?

ruby-on-rails - 如何检查 Rails 迁移中的数据库类型?

sql - 获取错误 "function week(timestamp without time zone) does not exist"