【问题标题】:Strange NoMethodError (undefined method `name' for nil:NilClass)奇怪的 NoMethodError(nil:NilClass 的未定义方法“名称”)
【发布时间】:2015-03-20 13:39:33
【问题描述】:

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

详细错误

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 &lt;&lt; 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 上正常运行。

【问题讨论】:

  • 看起来更像是一个表单标签正在接收一个 nil 对象或其他东西。不相关,但create_or_update_client 中的一些代码是可疑的;您正在控制器中执行仅模型代码,特别是 client_code 修改和徽标。
  • 感谢 @DaveNewton 提出必要的重构建议。我还更新了问题并添加了请求参数详细信息
  • rails 控制台加载 development 环境,看起来你在生产数据库中没有default_case_status(查询返回nil。)无论如何,它应该是一个环境问题。
  • @mudasobwa 我确信它只是生产环境。(RAILS_ENV=production rails c)。 database.yml 中除了生产数据库之外没有其他配置。独角兽也仅在生产环境中运行(bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production)
  • 您已将此标记为 Rails 3,所以我可以假设您使用的是 attr_accessor 而不是强参数吗?

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-09
    • 2013-04-25
    • 1970-01-01
    • 2013-11-14
    • 2013-01-14
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多