我有一个用于构建或编辑联系人对象的服务对象:
class BuildOrEditContact
include Service
def self.call(employee_json, company_id)
Contact.find_or_initialize_by(uid: employee_json['uid'] || employee_json['uuid']) do |c|
c.name = employee_json['name']
puts "Creating/updating #{c.name}."
c.email = employee_json['email']
c.job_title = employee_json['title']
c.bio = employee_json['bio']
c.phone = employee_json['phone']
c.avatar_url = employee_json['avatar_url']
c.company_id = company_id
c.save
puts "Saving #{c.name}, the #{c.job_title} of #{company_json['name']}"
end
end
end
运行正常,直到我添加了company_id和job_title行。
现在,当我在find_or_initialize by call之前使用binding.pry运行代码时,它会停止,我可以逐行运行代码。
单独地,线条工作正常。作为一个块,这是一个禁忌。
当我将整个块复制并粘贴到pry中时,它会运行除job_title和company_id行之外的所有内容。
如果我将binding.pry放在块内(c.name = employee_json['name']
之上),它就永远不会停止。代码运行就好像我没有做任何更改(没有job_title,company_id或puts)
导致这种选择性故障的原因是什么?
答案 0 :(得分:1)
确定。所以这解决了它:
我删除了该块并使用了find_or_initialize_by,因为您通常会看到它:
class BuildOrEditContact
include Service
def self.call(employee_json, company_id)
c = Contact.find_or_initialize_by(uid: employee_json['uid'] || employee_json['uuid'])
c.name = employee_json['name']
puts "Creating/updating #{c.name}."
c.email = employee_json['email']
c.job_title = employee_json['title']
c.bio = employee_json['bio']
c.phone = employee_json['phone']
c.avatar_url = employee_json['avatar_url']
c.company_id = company_id
c.save
puts "Saving #{c.name}, the #{c.job_title} of #{c.company.name}"
end
end