has_many :comments,:through => :commentings,:source => :commentable,:source_type => "Comment"accepts_nested_attributes_for :comments,:allow_destroy => truedef autosave_associated_records_for_comments comments.each do |comment| if existing_comment = Comment.find_by_fax_and_name(comment.fax,comment.name) self.comments.reject! { |hl| hl.fax == existing_comment.fax && hl.name == existing_comment.name } self.comments << existing_comment else self.comments << comment end endend
以下是相关的来源:https://github.com/rails/rails/blob/v3.0.11/activerecord/lib/active_record/autosave_association.rb#L155
解决方法 我已经找到了解决方案,但如果您知道更好的方法,请告诉我!def autosave_associated_records_for_comments existing_comments = [] new_comments = [] comments.each do |comment| if existing_comment = Comment.find_by_fax_and_name(comment.fax,comment.name) existing_comments << existing_comment else new_comments << comment end end self.comments << new_comments + existing_commentsend总结
以上是内存溢出为你收集整理的ruby-on-rails – has_many autosave_associated_records_for_ *全部内容,希望文章能够帮你解决ruby-on-rails – has_many autosave_associated_records_for_ *所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)