Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 强制ActiveScaffold删除关联而不是将外键设置为null_Ruby On Rails_Activerecord_Activescaffold - Fatal编程技术网

Ruby on rails 强制ActiveScaffold删除关联而不是将外键设置为null

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

我正在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

  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,并将其从数据库中删除。