奇怪的 NoMethodError(nil:NilClass 的未定义方法“名称”)

Strange NoMethodError (undefined method `name' for nil:NilClass)

部署到生产机器后开始出现此错误。这是简化的代码片段。

详细错误

NoMethodError (undefined method `name' for nil:NilClass):
  app/models/client.rb:276:in `add_cases_status_received'
  app/controllers/clients_controller.rb:145:in `create_or_update_client'
  app/controllers/clients_controller.rb:26:in `create'

app/models/client.rb

class Client < ActiveRecord::Base
  has_many :client_case_statuses, dependent: :destroy
  has_many :case_statuses, through: :client_case_statuses

  after_create :add_cases_status_received

  private

  def add_cases_status_received
    case_statuses << CaseStatus.default_case_status
  end
end

上述方法执行case_statuses << CaseStatus.default_case_status时出现错误

app/models/case_status.rb

class CaseStatus < ActiveRecord::Base
  has_many :client_case_statuses
  has_many :clients, through: :client_case_statuses

  attr_accessible :name
  validates_presence_of :name

  class << self
    def default_case_status
      find_by_name 'New'
    end
     . . .
  end
end

app/controllers/clients_controller.rb

class ClientsController < ApplicationController

  def create
    @client = Client.new(params[:client])
    create_or_update_client
  end

  private
  def create_or_update_client
    @client.client_code = @client.client_code.upcase
    if @client.valid?
      @client.company_logo = @logo
      if @client.save
        redirect_to client_edit_brand_page_path @client
      else
        render :new
      end
    else
      render :new
    end
  end
end

来自请求的参数

{"utf8"=>"✓", "authenticity_token"=>"VALID_TOKEN", "client"=>{"name"=>"amit", "client_code"=>"AS12344", "address"=>"", "total_employees"=>"", "about"=>"", "client_contact_attributes"=>{"id"=>"", "name"=>"test", "designation"=>"", "address"=>"", "mobile"=>"", "landline"=>"", "email"=>"amit@123.com"}}}

令人惊讶的是,如果我从 rails 控制台创建客户端记录,它会成功创建记录。

[1] pry(main)> CaseStatus.default_case_status
  CaseStatus Load (0.7ms)  SELECT "case_statuses".* FROM "case_statuses" WHERE "case_statuses"."name" = 'New' LIMIT 1
=> #<CaseStatus id: 15, name: "New", created_at: "2015-03-20 08:47:51", updated_at: "2015-03-20 08:47:51">
[2] pry(main)> c = Client.new({"name"=>"parthiv","client_code"=>"AS12345","address"=>"","total_employees"=>"","about"=>"","client_contact_attributes"=>{"id"=>"","name"=>"","designation"=>"","address"=>"","mobile"=>"","landline"=>"","email"=>"parthiv.savani@gmail.com"}})

=> #<Client id: nil, name: "parthiv", address: "", client_code: "AS12345", total_employees: nil, case_managed_by_client: nil, case_instructions: nil, about: "", can_delete_cases: nil, active: nil, created_at: nil, updated_at: nil, company_logo_file_name: nil, company_logo_content_type: nil, company_logo_file_size: nil, company_logo_updated_at: nil, code_of_ethics_file_name: nil, code_of_ethics_content_type: nil, code_of_ethics_file_size: nil, code_of_ethics_updated_at: nil>
[3] pry(main)> c.save!
   (0.6ms)  BEGIN
  Client Exists (1.2ms)  SELECT 1 AS one FROM "clients" WHERE "clients"."client_code" = 'AS12345' LIMIT 1
  SQL (6.4ms)  INSERT INTO "clients" ("about", "active", "address", "can_delete_cases", "case_instructions", "case_managed_by_client", "client_code", "code_of_ethics_content_type", "code_of_ethics_file_name", "code_of_ethics_file_size", "code_of_ethics_updated_at", "company_logo_content_type", "company_logo_file_name", "company_logo_file_size", "company_logo_updated_at", "created_at", "name", "total_employees", "updated_at") VALUES (, , , , , , , , , , , , , , , , , , ) RETURNING "id"  [["about", ""], ["active", nil], ["address", ""], ["can_delete_cases", nil], ["case_instructions", nil], ["case_managed_by_client", nil], ["client_code", "AS12345"], ["code_of_ethics_content_type", nil], ["code_of_ethics_file_name", nil], ["code_of_ethics_file_size", nil], ["code_of_ethics_updated_at", nil], ["company_logo_content_type", nil], ["company_logo_file_name", nil], ["company_logo_file_size", nil], ["company_logo_updated_at", nil], ["created_at", Fri, 20 Mar 2015 18:09:40 IST +05:30], ["name", "parthiv"], ["total_employees", nil], ["updated_at", Fri, 20 Mar 2015 18:09:40 IST +05:30]]
  SQL (1.3ms)  INSERT INTO "client_contacts" ("address", "client_id", "created_at", "designation", "email", "landline", "mobile", "name", "primary", "updated_at") VALUES (, , , , , , , , , ) RETURNING "id"  [["address", ""], ["client_id", 26], ["created_at", Fri, 20 Mar 2015 18:09:40 IST +05:30], ["designation", ""], ["email", "parthiv.savani@gmail.com"], ["landline", ""], ["mobile", ""], ["name", ""], ["primary", nil], ["updated_at", Fri, 20 Mar 2015 18:09:40 IST +05:30]]

  CaseStatus Load (0.9ms)  SELECT "case_statuses".* FROM "case_statuses" WHERE "case_statuses"."name" = 'New' LIMIT 1
  SQL (1.4ms)  INSERT INTO "client_case_statuses" ("active", "case_status_id", "client_id", "created_at", "updated_at") VALUES (, , , , ) RETURNING "id"  [["active", true], ["case_status_id", 15], ["client_id", 26], ["created_at", Fri, 20 Mar 2015 18:09:53 IST +05:30], ["updated_at", Fri, 20 Mar 2015 18:09:53 IST +05:30]]
   (1.4ms)  COMMIT
=> true

即使相同的代码库在其他 VPS.

上也能正常工作

我探索了很多但没有线索。我什至检查了是否有任何待处理的迁移,甚至再次 运行 迁移,但仍然出现相同的错误。

最后我再次删除了数据库和 运行 迁移。这解决了问题。