Ruby on rails 强制ActiveScaffold删除关联而不是将外键设置为null
我正在ROR2.2项目中使用ActiveScaffold。我的应用程序中有两种型号:Ruby on rails 强制ActiveScaffold删除关联而不是将外键设置为null,ruby-on-rails,activerecord,activescaffold,Ruby On Rails,Activerecord,Activescaffold,我正在ROR2.2项目中使用ActiveScaffold。我的应用程序中有两种型号: class Foo << ActiveRecord::Base belongs_to :bar end class Bar << ActiveRecord::Base has_many :foos end class Foo类似于以下内容的内容应该可以达到您想要的效果。请记住,我还没有运行或测试过这段代码 class Bar < ActiveRecord::Base
class Foo << ActiveRecord::Base
belongs_to :bar
end
class Bar << ActiveRecord::Base
has_many :foos
end
class Foo类似于以下内容的内容应该可以达到您想要的效果。请记住,我还没有运行或测试过这段代码
class Bar < ActiveRecord::Base
has_many :foos, :dependent => :destroy, :after_remove => :delete_orphan
def delete_orphan(foo)
foo.destroy
end
end
类栏:destroy,:after\u remove=>:delete\u孤立
def delete_孤儿(foo)
破坏
结束
结束
编辑:切换到更具体的回调我在Rails 3.1中使用了它
当我删除一个文档时,所有相关的DocumentFoo也会被删除
class Document < ActiveRecord::Base
has_many :document_foos
before_destroy { |record| DocumentFoo.destroy_all "document_id = #{record.id}" }
end
class文档
比尔,
乔纳斯这是一个积极的问题。它会自动在后台为你生成很多东西。现在在我提到的场景中,delete通过将Foo.bar_id设置为null来工作。我想从工具栏中删除一个foo,并将其从数据库中删除。