奇怪的NoMethodError(未定义的方法`name'代表nil:NilClass)

时间:2015-03-20 13:39:34

标签: 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'

应用/模型/ 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时出错。

应用/模型/ 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

应用/控制器/ 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。

1 个答案:

答案 0 :(得分:0)

我探索了很多但不是线索。我甚至检查了是否有任何迁移暂挂,甚至再次运行迁移但仍然得到相同的错误。

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