[英]Ruby 2 & Rails 4 Query returns results in development, but return nil in production
We are writing an application in ruby on rails and we are experiencing an oddity. 我们正在用Rails编写红宝石应用程序,但遇到了奇怪的情况。 When we run the app in development our query returns just fine.
当我们在开发中运行该应用程序时,查询返回就很好。 When we run the app in production our query returns nil, even with records in the database.
在生产环境中运行应用程序时,即使数据库中有记录,查询也会返回nil。 I have queried directly through MySQL Server so I know the records are there.
我直接通过MySQL Server查询,所以我知道记录在那里。
Any Ideas? 有任何想法吗? I have included the error, migration, model, controller and view code.
我已经包括了错误,迁移,模型,控制器和视图代码。
The error I get is 我得到的错误是
undefined method `each' for nil:NilClass
Extracted source (around line #19): 提取的源(第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>
Migration File 迁移档案
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
Model 模型
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
View 视图
<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 rails console生产结果
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">]>
Queries like this where("hot=0 AND active=1")
are depending on a certain database, because not all databases encode true
and false
as 1
or 0
. 这样的查询
where("hot=0 AND active=1")
取决于某个数据库,因为并非所有数据库都将true
和false
编码为1
或0
。
Write your queries like this: 这样编写查询:
Lead.where(hot: false, active: true)
It is usually something stupid, if u say it works on dev. 如果您说它对开发人员有效,通常这是愚蠢的。 Try checking:
尝试检查:
Temporarily activate debug and check logs for clues or dump the database and try the prod dump carefully in dev 临时激活调试并检查日志以获取线索或转储数据库,然后在开发人员中仔细尝试生产转储
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.