vendredi 20 mars 2015

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

This error started appearing after deploying to production machine. Here is simplified code snippet.


detailed error



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


The error comes while executing case_statuses << CaseStatus.default_case_status in above method.


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


Parameters from request



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


Surprisingly if I create client record from rails console, it creates record successfully.



[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


Even same codebase works fine on other VPS.


Aucun commentaire:

Enregistrer un commentaire