我们正在ruby上编写一个应用程序,我们正在经历一个奇怪的问题。 当我们在开发中运行应用程序时,我们的查询返回正常。 当我们在生产中运行应用程序时,即使数据库中有记录,我们的查询也会返回nil。我直接通过MySQL服务器查询,所以我知道记录在那里。
任何想法?我已经包含了错误,迁移,模型,控制器和视图代码。
我得到的错误是
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
控制器
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 console production results
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">]>
答案 0 :(得分:2)
此where("hot=0 AND active=1")
之类的查询取决于某个数据库,因为并非所有数据库都将true
和false
编码为1
或0
。
按如下方式编写查询:
Lead.where(hot: false, active: true)
答案 1 :(得分:1)
如果你说它适用于dev,那通常是愚蠢的。尝试检查:
暂时激活调试并检查日志以获取线索或转储数据库并在dev中仔细尝试prod转储