在我的Rails 6应用程序中,我有菜单,它们有页面。页面的"content“属性中有ActionText。
models/menu.rb
class Menu < ApplicationRecord
has_and_belongs_to_many :pages
endmodels/page.rb
class Page < ApplicationRecord
has_and_belongs_to_many :menus
has_rich_text :content
end我如何复制(例如,真正复制)页面,并保持它们的菜单关联和action_text内容?AR的.dup破坏了关联和内容。deep_clone对AT内容只字未提,阿米巴似乎死了?
我的东西还有一些active_storage附件,顺便说一下:)
发布于 2022-10-27 18:28:22
看看ActiveRecord的dup方法,可以得到一个很好的解释。它只提供一个“浅”副本,因为“深度”副本的范围是特定于应用程序的。
只需覆盖模型中的dup,以包含所需的关联。我经常在名字和清晰的鼻涕虫后面加上“(复制)”。
下面是一个让你开始的例子:
class Page < ApplicationRecord
has_and_belongs_to_many :menus
has_rich_text :content
# Deep duplicate (copy)
def dup
super.tap do |duplicate|
duplicate.attributes = {
title: title.concat(" (copy)"), # change duplicate's title
slug: nil, # clear the slug if you're using friendly_id
content: content.dup,
menu_ids: menu_ids
}
end
end
endhttps://stackoverflow.com/questions/60366062
复制相似问题