Ruby 2.2,Rails 3.21
当我试图破坏我的对象时,我收到以下错误。
对于模型与:dependent => :restrict
选项
这个错误很奇怪,因为它建议使用NIL对象,但是当我在错误之前记录相关对象的类和Id时,它会显示具有正确ID的预期类。
Projects :: ProjClefsRepartitionController中的NoMethodError#destroy
未定义的方法`name'为零:NilClass
应用程序/控制器/项目/ proj_clefs_repartition_controller.rb:322:在 阻止破坏
app / controllers / projects / proj_clefs_repartition_controller.rb:315:in destroy
这是ProjClefsRepartitionController控制器的相关代码:
294# def destroy
295#
296# messages = {}
297#
298# # Create the @proj_clef_repartition objet
299# unless creer_instance_objet_et_render_parent_si_impossible(ProjClefRepartition, params[:id], params[:proj_sous_projet_id], [{:proj_sous_projet => [:proj_charges, {:proj_projet => {:proj_mesure => :proj_paquet_mesures}}]}]) then
300#
301# enregistrement_ok = false
302#
303# @objet_de_l_erreur = @proj_clef_repartition
304# @proj_sous_projet = @proj_clef_repartition.proj_sous_projet
305# @proj_projet = @proj_sous_projet.proj_projet
306# @proj_mesure = @proj_projet.proj_mesure
307# @proj_paquet_mesures = @proj_mesure.proj_paquet_mesures
308# @hist_version_sous_projet = HistVersionSousProjet.where(:proj_sous_projet_id => @proj_sous_projet.id).order{version.desc}.limit(1)[0]
309#
310# @proj_clefs_repartition = @proj_sous_projet.proj_clefs_repartition
311# @proj_charges = @proj_sous_projet.proj_charges
312# @total_charges = BigDecimal.new('0', 18)
313#
314# # =================== DEBUT DE LA TRANSACTION ===================
315# ProjClefRepartition.transaction do
316#
317#
318# if sauver_version(@proj_clef_repartition, messages, nil, 3, nil, true) then
319#
320# logger.debug "#{@proj_clef_repartition.blank?}"
321# logger.debug "#{@proj_clef_repartition.class} (#{@proj_clef_repartition.id})"
322# if @proj_clef_repartition.destroy then
323# ajouter_message(t('activerecord.successful.messages.deleted', :model => ProjClefRepartition.model_name.human), :success, messages)
324# enregistrement_ok = true
325# else
326# ajouter_message(t('activerecord.warning.messages.deleted', :model => ProjClefRepartition.model_name.human), :alert, messages)
327# end # proj_clef_repartition.destroy
328#
329# end # sauver_version(@proj_clef_repartition, messages, nil, 3, nil, true)
330#
331#
332# raise ActiveRecord::Rollback unless enregistrement_ok
333#
334# end # ProjClefRepartition.transaction
335# # =================== FIN DE LA TRANSACTION ===================
336#
337#
338# afficher_maintenant(messages)
339#
340# render 'projects/proj_sous_projets/show'
341#
342# end # creer_instance_objet_et_render_parent_si_impossible(ProjClefRepartition, params[:id], params[:proj_sous_projet_id])
343#
344#
345# rescue ActiveRecord::StatementInvalid => exception
346#
347#
348#
349#
350#
351# @hist_sous_projet = @hist_version_sous_projet.hist_sous_projet
352# ajouter_message(t('activerecord.warning.messages.invalid_db_statement'), :error, messages)
353# ajouter_erreur_concernant_exception_statement_invalid(exception, @objet_de_l_erreur)
354# afficher_maintenant(messages)
355# render 'projects/proj_sous_projets/show'
356#
357#
358# rescue ActiveRecord::DeleteRestrictionError => exception
359#
360#
361# @hist_sous_projet = @hist_version_sous_projet.hist_sous_projet
362#
363# ajouter_message(t('activerecord.warning.messages.delete_restriction', :model => ProjClefRepartition.model_name.human), :error, messages)
364# afficher_maintenant(messages)
365# render 'projects/proj_sous_projets/show'
366#
367# end # destroy
以下是与错误相关的日志摘录。 您可以看到该对象不是空白的,并且具有一个具有有效ID的类。
它显示false
日志@proj_clef_repartition.blank?
。
它显示ProjClefRepartition (96)
和@proj_clef_repartition.class
@proj_clef_repartition.id
QUITTING ||| historisation_nouvelle_version_avec_origine (pub_modification_id = 2)||| ===> output_hash [:id_hist_origine_modification] = 1202(0.4ms)SELECT COUNT(*)FROM" proj_charges"哪里 " proj_charges"" proj_clef_repartition_id" = 96
假
ProjClefRepartition(96)
(0.4ms)ROLLBACK
在298.9ms完成500内部服务器错误
NoMethodError(未定义的方法
中阻止name' for nil:NilClass):
在destroy'
app/controllers/projects/proj_clefs_repartition_controller.rb:322:in应用程序/控制器/项目/ proj_clefs_repartition_controller.rb:315:在 `破坏'
渲染 /home/douglas/.rvm/gems/ruby-2.2.0@rail3/gems/actionpack-3.2.21/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.4ms)呈现 /home/douglas/.rvm/gems/ruby-2.2.0@rail3/gems/actionpack-3.2.21/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.9ms)呈现 /home/douglas/.rvm/gems/ruby-2.2.0@rail3/gems/actionpack-3.2.21/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb 在救援/布局(21.8ms)内
没有意义。我检查了完整的痕迹,注意到" Nil"在active_record has_many关联中发生的对象错误。 这是完整的痕迹:
Projects :: ProjClefsRepartitionController中的NoMethodError#destroy
未定义的方法`name' for nil:NilClass Rails.root:/ var / www / odpf
应用程序跟踪|框架跟踪|完整跟踪
activerecord(3.2.21) LIB / active_record /协会/ has_many_association.rb:58:在 `cached_counter_attribute_name'
activerecord(3.2.21) LIB / active_record /协会/ has_many_association.rb:54:在 `has_cached_counter'?
activerecord(3.2.21) LIB / active_record /协会/ has_many_association.rb:37:在 `count_records'
activerecord(3.2.21) lib / active_record / associations / collection_association.rb:261:in`size'
activerecord(3.2.21) LIB / active_record /协会/ collection_association.rb:280:在 `空'?
activerecord(3.2.21) lib / active_record / associations / collection_proxy.rb:46:在`空?'
activerecord(3.2.21) lib / active_record / associations / builder / has_many.rb:63:在`block in define_restrict_dependency_method'
activesupport(3.2.21)lib / active_support / callbacks.rb:418:in `_run__1417810521555210605__destroy__1154072583585286936__callbacks'
activesupport(3.2.21)lib / active_support / callbacks.rb:405:in `__run_callback'
activesupport(3.2.21)lib / active_support / callbacks.rb:385:in `_run_destroy_callbacks'
activesupport(3.2.21)lib / active_support / callbacks.rb:81:in `run_callbacks'
activerecord(3.2.21)lib / active_record / callbacks.rb:254:在`destroy'
activerecord(3.2.21)lib / active_record / transactions.rb:254:在`block中 在销毁'
activerecord(3.2.21)lib / active_record / transactions.rb:313:在`block中 在with_transaction_returning_status'
activerecord(3.2.21) LIB / active_record / connection_adapters /抽象/ database_statements.rb:192:在 `交易'
activerecord(3.2.21)lib / active_record / transactions.rb:208:in `交易'
activerecord(3.2.21)lib / active_record / transactions.rb:311:in `with_transaction_returning_status'
activerecord(3.2.21)lib / active_record / transactions.rb:254:in `破坏'
应用程序/控制器/项目/ proj_clefs_repartition_controller.rb:322:在 `阻止破坏'
activerecord(3.2.21) LIB / active_record / connection_adapters /抽象/ database_statements.rb:192:在 `交易'
activerecord(3.2.21)lib / active_record / transactions.rb:208:in `交易'
应用程序/控制器/项目/ proj_clefs_repartition_controller.rb:315:在 `破坏'
所以我检查了我的模型ProjClefRepartition。
当我删除了两行的, :dependent => :restrict
部分时,我做了一个没有错误的成功测试:
has_many :proj_contributions, :dependent => :restrict
has_many :proj_charges, :dependent => :restrict
require Rails.root.to_s + '/lib/opf_modules/opf_outils_models.rb'
class ProjClefRepartition < ActiveRecord::Base
include OutilsModels
# Renforcement de la sécurité avec declarative_authorization Gem
using_access_control
# Modifier les attributs contenant des string vides en des nil
before_validation :blank_string_attributes_to_nil
attr_accessible :code, :designation, :proj_sous_projet_id, :motivation, :pub_modification_id
validates :code, :designation, :proj_sous_projet_id, :pub_modification_id, :admin_utilisateur_id, :presence => true
attr_accessor :montant_contributions_total
validates :code, :length => { :in => 2..19 }, :uniqueness => { :case_sensitive => false }
validates :designation, :length => { :in => 2..60 }
validates :motivation, :length => { :in => 5..600 }, :allow_blank => true
validate :clef_repartition_exclusive_a_un_unique_sous_projet
belongs_to :proj_sous_projet
belongs_to :pub_modification
belongs_to :admin_utilisateur
has_many :proj_contributions, :dependent => :restrict
has_many :proj_charges, :dependent => :restrict
has_many :hist_versions_clefs_repartition, :class_name => HistVersionClefRepartition
# Méthode utilisée par la collection_select box
def texte_pour_collection_select
# Groupé les attributs afin d'améliorer la compréhension de l'utilisateur lors du choix par la select box
self.code + ' - ' + self.designation
end
# -------------------------------------------------------------
# Validations speciale
# -------------------------------------------------------------
private
def clef_repartition_exclusive_a_un_unique_sous_projet
# Lors de la création d'une nouvelle clé de répartition, l'ID est nil, et il n'y a aucun risque que la clef soit déjà utilisée par une charge
unless id.nil? then
# Trouver toutes les charges utilisant cette clé de répartition
proj_charges = ProjCharge.where(:proj_clef_repartition_id => id)
proj_charges.each do |c|
if c.proj_sous_projet_id != proj_sous_projet_id then
errors.add(:proj_sous_projet_id, I18n.t('activemodel.warning.messages.reaffectation_impossible', :modele_enfant => I18n.t('projects.proj_charges.model'), :modele_parent => I18n.t('projects.proj_sous_projets.model'), :modele => I18n.t('projects.proj_clefs_repartition.model')))
break
end # c.proj_sous_projet_id != proj_sous_projet_id
end # proj_charges.each
end # id.nil?
end # clef_repartition_exclusive_a_un_unique_sous_projet
end
所以我真的很想知道这可能是Ruby on Rails版本3.21中的错误。
任何帮助都非常受欢迎。
===编辑===
另外两个相关模型(proj_charge和proj_contribution)确实有以下几行:
belongs_to :proj_clef_repartition