我如何摧毁一个加载ActiveModel实例与相关的破坏有一个默认的排序孩子吗? [英] How do I destroy a ActiveModel instance with dependant destroy children that have a default ordering?
问题描述
在Rails 4.0我有2 ActiveRecord的类:
In Rails 4.0 I have 2 ActiveRecord classes:
class Sequence < ActiveRecord::Base
has_many :steps, dependent: :destroy
end
和
class Steps < ActiveRecord::Base
belongs_to :sequence
default_scope -> { order('order ASC') }
end
当我称之为 mySequence.destroy
我得到这个错误:
When I call mySequence.destroy
I get this error:
PG ::语法错误:错误:语法错误或接近订单行1:...步骤WHERE步骤sequence_id= $ 1 ORDER BY为了ASC ^:选择。。* FROM的步骤步骤WHERE台阶。sequence_id= $ 1目承ASC
PG::SyntaxError: ERROR: syntax error at or near "order" LINE 1: ...steps" WHERE "steps"."sequence_id" = $1 ORDER BY order ASC ^ : SELECT "steps".* FROM "steps" WHERE "steps"."sequence_id" = $1 ORDER BY order ASC
当我删除默认的范围,错误走了,但我明明已经下令在我的code在序列中的步骤。 我曾尝试这样定义的关联,并留下了default_scope语句:
When I remove the default scope, the error is gone, but I obviously have to order the steps in my sequence in my code. I did try to define the association like this, and leaving out the default_scope statement:
class Sequence < ActiveRecord::Base
has_many :steps, dependent: :destroy, order: 'order ASC'
end
但它抛出了同样的错误。
but it threw the same error.
没有任何人有这个问题?这是Rails中的错误?我想象中的排序不需要在对孩子的破坏SQL语句。
Does anyone else have this issue? Is this a bug in Rails? I imagine the ordering is not needed in the destroy sql statement on the children.
推荐答案
我贴这个问题上轨GitHub的问题页面上也是如此。 GitHub的用户tanraya 建议采取以下优雅的解决方案。
I posted this issue on the rails gitHub issue page as well. gitHub user tanraya suggested the following elegant solution.
default_scope -> { order order: :asc }
由于从看过来,tanraya和赞誉!
Thanks from over here, tanraya, and kudos!
这篇关于我如何摧毁一个加载ActiveModel实例与相关的破坏有一个默认的排序孩子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!