ruby - 奇怪的 NoMethodError(未定义方法 `name' 为 nil :NilClass)

标签 ruby ruby-on-rails-3 activerecord rails-activerecord

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

详细错误

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 ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19) 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 ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) 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 ($1, $2, $3, $4, $5) 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 上也能正常工作。

最佳答案

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

最后我放弃了数据库并再次运行迁移。这解决了问题。

关于ruby - 奇怪的 NoMethodError(未定义方法 `name' 为 nil :NilClass),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29167915/

相关文章:

ruby-on-rails - 如果模型在深层命名空间中,set_table_name 不起作用 - Rails 2.3.14 中的错误?

php - Codeigniter 中的嵌套连接

ruby - 如何记录 Ruby 代码?

ruby - Number Bases & Ruby - 递增用于特定数字基数的变量的最快方法是什么?

ruby-on-rails - 如何从 ActiveRecord 中名为 "object_id"的列中检索值?

ruby - Mac OS X Lion 上的 sqlite3 gem...失败!

javascript - 如何使用 Ruby 查找 DOM 元素是否具有事件监听器

ruby-on-rails - 如何更改 Ruby on Rails 应用程序名称?

ruby-on-rails - 无法将Rails应用程序连接到Oracle

ruby-on-rails - 测试使用 ActiveRecord 模型的 gem