简体   繁体   中英

Rails 6 - NoMethodError (undefined method `titleize' for nil:NilClass)

I looked through questions here about my error, but they are seems unrelated, hence this post. I have a 'capitalize_names' method in my UserProfile model that throw this error when I update attributes in the console. But the model gets updated even though I get this error. I tried making my instance method private, protected. But the results is the same. Not sure what I'm missing here. Big thank you in advance for your help.

My model:

class UserProfile < ApplicationRecord
  belongs_to :user
  before_update :capitalize_names

 # i tried protected or private still gets the same result!!
 def capitalize_names
   self.first_name = self.first_name.titleize
   self.last_name = self.last_name.titleize
 end
end

Here's my console out put:

irb(main):004:0> u
=> #<User id: 25, username: "somename1", email: "bhati12345@gmail.com", created_at: "2019-09-14 14:09:46", updated_at: "2019-09-14 14:09:46">

irb(main):005:0> u.profile
=> #<UserProfile id: 25, first_name: nil, last_name: nil, user_id: 25, pop_value: nil, time_zone: nil, country: nil, sketch_profile: {}, profile_image: nil, logo: nil, created_at: "2019-09-14 14:09:46", updated_at: "2019-09-14 14:09:46", gender: "male">

irb(main):006:0> u.profile.update(first_name: 'somecool name')
Traceback (most recent call last):
    2: from (irb):6
    1: from app/models/user_profile.rb:16:in `capitalize_names'
NoMethodError (undefined method `titleize' for nil:NilClass)

But, as you can see my model still got successfully updated...

irb(main):007:0> u.profile
=> #<UserProfile id: 25, first_name: "Somecool Name", last_name: nil, user_id: 25, pop_value: nil, time_zone: nil, country: nil, sketch_profile: {}, profile_image: nil, logo: nil, created_at: "2019-09-14 14:09:46", updated_at: "2019-09-14 14:10:52", gender: "male">

The problem is your second call to titleize , because in the example self.last_name is nil . You cannot call titleize on nil, but your record still gets modified because the first call to titleize is for self.first_name which is a string. To avoid the exception you could set the default value of both fields to an empty string, or check they aren't nil

 def capitalize_names
   self.first_name = self.first_name.try(:titleize)
   self.last_name = self.last_name.titleize unless self.last_name.nil?
 end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM