sách gpt4 ăn đã đi

ruby-on-rails - has_many autosave_associated_records_for_*

In lại 作者:数据小太阳 更新时间:2023-10-29 07:27:27 25 4
mua khóa gpt4 giày nike

我正在尝试关联现有记录,同时仍然能够添加新记录。以下不起作用,但非常接近我需要的。如何完成关联现有记录和创建新记录?

has_many :comments, :through => :commentings, :source => :commentable, :source_type => "Comment"
accepts_nested_attributes_for :comments, :allow_destroy => true

def 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
khác
self.comments << comment
kết thúc
kết thúc
kết thúc

这是相关的源代码行:https://github.com/rails/rails/blob/v3.0.11/activerecord/lib/active_record/autosave_association.rb#L155

câu trả lời hay nhất

我已经制定了解决方案,但如果您知道更好的方法,请告诉我!

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
khác
new_comments << comment
kết thúc
kết thúc

self.comments << new_comments + existing_comments
kết thúc

关于ruby-on-rails - has_many autosave_associated_records_for_*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9024745/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress