:dependent =>:destroy会破坏不再依赖的记录(:dependent => :destroy destroys records that are no longer dependents)
在PostgreSQL上运行的Rails 4.2
我正在重构我的数据库并移动一些数据。 每个
ProgressUpdate
属于一个LanguageProgress
。 在迁移中,我将一些ProgressUpdates
从一个LanguageProgress
移动到另一个,然后我销毁了不再有任何关联ProgressUpdates
的LanguageProgress
,但是destroy会触发曾经依赖它的ProgressUpdates
上的destroy! 为什么?这是迁移中的相关代码:
LanguageProgress.find_each do |lp| if existing_lp = LanguageProgress.find_by(progress_marker: lp.progress_marker, language_id: lp.state_language.language_id) puts "LanguageProgress #{existing_lp.id} has these ProgressUpdates: #{existing_lp.progress_updates.pluck :id}" puts "LanguageProgress #{lp.id} has these ProgressUpdates: #{lp.progress_updates.pluck :id}" lp.progress_updates.each do |pu| pu.language_progress = existing_lp pu.save! end puts "now LanguageProgress #{existing_lp.id} has these ProgressUpdates: #{existing_lp.progress_updates.pluck :id}" puts "and LanguageProgress #{lp.id} has #{lp.progress_updates.count} ProgressUpdates" puts "destroying LanguageProgress #{lp.id}" lp.destroy else lp.language_id = lp.state_language.language_id lp.save! end end
这是打印出来的:
LanguageProgress 1073685945 has these ProgressUpdates: [1071256240] LanguageProgress 1073691483 has these ProgressUpdates: [1071256239] now LanguageProgress 1073685945 has these ProgressUpdates: [1071256239, 1071256240] and LanguageProgress 1073691483 has 0 ProgressUpdates destroying LanguageProgress 1073691483 destroying ProgressUpdate 1071256239
这是
language_progress.rb
的相关位class LanguageProgress < ActiveRecord::Base belongs_to :language belongs_to :state_language belongs_to :progress_marker has_many :progress_updates, dependent: :destroy validates :progress_marker, presence: true, uniqueness: { scope: :state_language } validates :state_language, presence: true
和
progress_update.rb
class ProgressUpdate < ActiveRecord::Base belongs_to :user belongs_to :language_progress delegate :language, to: :language_progress validates :progress, presence: true, inclusion: ProgressMarker.spread_text.keys validates :month, presence: true, inclusion: 1..12 validates :year, presence: true, inclusion: 2000..Time.now.year validates :user, presence: true validates :language_progress, presence: true before_destroy{ |pu| puts "destroying ProgressUpdate #{pu.id}" } end
Rails 4.2 running on PostgreSQL
I'm refactoring my database and moving some data around. Each
ProgressUpdate
belongs to aLanguageProgress
. In a migration I move someProgressUpdates
from oneLanguageProgress
to another, then I destroy theLanguageProgress
which no longer has any associatedProgressUpdates
, however the destroy triggers the destroy on theProgressUpdates
that used to be dependent on it! Why's that?Here's the relevant code in the migration:
LanguageProgress.find_each do |lp| if existing_lp = LanguageProgress.find_by(progress_marker: lp.progress_marker, language_id: lp.state_language.language_id) puts "LanguageProgress #{existing_lp.id} has these ProgressUpdates: #{existing_lp.progress_updates.pluck :id}" puts "LanguageProgress #{lp.id} has these ProgressUpdates: #{lp.progress_updates.pluck :id}" lp.progress_updates.each do |pu| pu.language_progress = existing_lp pu.save! end puts "now LanguageProgress #{existing_lp.id} has these ProgressUpdates: #{existing_lp.progress_updates.pluck :id}" puts "and LanguageProgress #{lp.id} has #{lp.progress_updates.count} ProgressUpdates" puts "destroying LanguageProgress #{lp.id}" lp.destroy else lp.language_id = lp.state_language.language_id lp.save! end end
Here's what it prints out:
LanguageProgress 1073685945 has these ProgressUpdates: [1071256240] LanguageProgress 1073691483 has these ProgressUpdates: [1071256239] now LanguageProgress 1073685945 has these ProgressUpdates: [1071256239, 1071256240] and LanguageProgress 1073691483 has 0 ProgressUpdates destroying LanguageProgress 1073691483 destroying ProgressUpdate 1071256239
Here's the relevant bits of
language_progress.rb
class LanguageProgress < ActiveRecord::Base belongs_to :language belongs_to :state_language belongs_to :progress_marker has_many :progress_updates, dependent: :destroy validates :progress_marker, presence: true, uniqueness: { scope: :state_language } validates :state_language, presence: true
and
progress_update.rb
class ProgressUpdate < ActiveRecord::Base belongs_to :user belongs_to :language_progress delegate :language, to: :language_progress validates :progress, presence: true, inclusion: ProgressMarker.spread_text.keys validates :month, presence: true, inclusion: 1..12 validates :year, presence: true, inclusion: 2000..Time.now.year validates :user, presence: true validates :language_progress, presence: true before_destroy{ |pu| puts "destroying ProgressUpdate #{pu.id}" } end
原文:https://stackoverflow.com/questions/35470942
最满意答案
您可以使用列表理解来加入列表:
print '\n'.join([''.join(inner) for inner in mlist])
列表理解创建每个嵌套列表的一个字符串,然后我们用新行将这个新的行列表加入到一个更大的字符串中。
演示:
>>> mlist = [['#', '#', '#', '#', '#'], ['#', '0', ' ', ' ', '#'], ['#', '#', '#', ' ', '#']] >>> print '\n'.join([''.join(inner) for inner in mlist]) ##### #0 # ### #
你也可以使用
for
循环:for inner in mlist: print ''.join(inner)
You can use a list comprehension to join lists:
print '\n'.join([''.join(inner) for inner in mlist])
The list comprehension creates a string of each nested list, then we join that new list of rows into a larger string with newlines.
Demo:
>>> mlist = [['#', '#', '#', '#', '#'], ['#', '0', ' ', ' ', '#'], ['#', '#', '#', ' ', '#']] >>> print '\n'.join([''.join(inner) for inner in mlist]) ##### #0 # ### #
You could also have used a
for
loop:for inner in mlist: print ''.join(inner)
相关问答
更多-
具有compare功能,根据元素是否相等返回True或False是不理想的。 基本上,您必须将每个元素与每个现有组中的一些“原型”进行比较。 像这样的东西: def group(elements, comp_func): groups = {} for x in elements: for y in groups: if comp_func(x, y): groups[y].append(x) ...
-
您可以使用列表理解来加入列表: print '\n'.join([''.join(inner) for inner in mlist]) 列表理解创建每个嵌套列表的一个字符串,然后我们用新行将这个新的行列表加入到一个更大的字符串中。 演示: >>> mlist = [['#', '#', '#', '#', '#'], ['#', '0', ' ', ' ', '#'], ['#', '#', '#', ' ', '#']] >>> print '\n'.join([''.join(inner) for ...
-
您可以将元素的长度作为集合与原始列表的长度进行比较: d = [[5,5,5],[5,10,5],[5,10,15],[5,10,10],[5,10,20]] new_d = [i for i in d if len(set(i)) == len(i)] 输出: [[5, 10, 15], [5, 10, 20]] You can compare the length of an element as a set to the length of the original list: d = [[5,5 ...
-
lapply(assignments,function(x) x[x!=0]) lapply(assignments,function(x) x[x!=0])
-
列表中的元素。(Elements within lists.)[2022-11-15]
对于你的第一个问题: which(sapply(List, `%in%`, x = 3)) # b # 2 对于第二个问题,您可以使用类似这样的函数: list.to.df <- function(arg.list) { max.len <- max(sapply(arg.list, length)) arg.list <- lapply(arg.list, `length<-`, max.len) as.data.frame(arg.list) } list.to.df(List ... -
从列表列表中获取元素(Get elements from list of lists)[2023-09-06]
在SWI-Prolog(以及其他人)中,您可以使用flatten/2 : ?- flatten([[[a,b,[c]],d,e],f,g,[h,[i,j]]], S). S = [a, b, c, d, e, f, g, h, i|...]. 请注意, flatten/2的SWI-Prolog手册页包含以下声明: 结束需要展平/ 3通常表示,如追加/ 3附加两个列表,一个糟糕的设计。 但是,该页面没有说明是否有另一个本机谓词来替换它。 我相信会提供更好的答案。 In SWI-Prolog (and may ... -
import itertools as it a = [[1, 2], [4, 5, 6], [], [7, 12, 14, 16]] reversed(it.islice(it.chain.from_iterable(reversed(a)), 5)) 这实际上假设a中没有None 。 如果只是做a = filter(a, None) 。 import itertools as it a = [[1, 2], [4, 5, 6], [], [7, 12, 14, 16]] reversed(it.i ...
-
我看到 map (!! 1) matrix 将返回行的第二个元素的列表,即矩阵的第二列; 但我不知道如何在一个很好的函数中将它扩展到矩阵的所有列。 如果这是您想要的方式,您可以简单地将其更改为 map (!!i) matrix 在 [map (!!i) matrix | i <- [0.. length (matrix!!0) - 1]] 例如 Prelude> let matrix = [[1,2,3],[4,5,6]] Prelude> [map (!!i) matrix | i <- [0.. ...
-
您可以使用以下生成器函数来生成重复元素的索引。 请注意,这仍然适用于简单的整数列表,列表或列表的列表。 >>> def index_of_list_element_duplicates(x): ... seen = set() ... for index, element in enumerate(x): ... if isinstance(element, list): ... element = tuple(element) ... ...
-
将子列表转换为元组,例如: In [2]: a = [[2,3],[5,6],[8,9]] In [3]: b = [[2,3],[5,6],[8,9], [10,11]] In [4]: set(tuple(x) for x in a).issubset(tuple(x) for x in b) Out[4]: True Convert your sublists to tuples, for example: In [2]: a = [[2,3],[5,6],[8,9]] In [3]: b = ...