Rails 3.1.0 服务器在尝试删除关联模型时崩溃

发布于 2024-12-06 20:44:33 字数 1172 浏览 3 评论 0原文

我有以下模型:

Association.rb:

class Association < ActiveRecord::Base
  has_and_belongs_to_many :users
  has_many :condominia, :dependent => :destroy
  accepts_nested_attributes_for :condominia, :allow_destroy => true
end

condominium.rb

class Condominium < ActiveRecord::Base
  belongs_to :association
  has_many :owners, :dependent => :destroy
  accepts_nested_attributes_for :owners, :allow_destroy => true
end

Owner.rb

class Owner < ActiveRecord::Base
  belongs_to :condominium
  belongs_to :user
end

当我尝试删除关联模型时,我的 Rails 服务器崩溃了。 Rails 版本 3.1.0 。和 Ruby 1.9.2 p290 有什么想法为什么会发生这种情况吗?

谢谢。

更新:如果我尝试删除 :dependent => :摧毁它的作品。但是因为我需要在构建 Condominium 对象时创建一批所有者,所以我添加了:

def new_owners
    return 0
  end

  def new_owners=(int_num)
    int_num = int_num.to_i
    if int_num > 0
      int_num.times do
        self.owners.create
      end
    end
  end

结果是相同的。 Rails 服务器在保存时崩溃。

I have the following models:

association.rb:

class Association < ActiveRecord::Base
  has_and_belongs_to_many :users
  has_many :condominia, :dependent => :destroy
  accepts_nested_attributes_for :condominia, :allow_destroy => true
end

condominium.rb

class Condominium < ActiveRecord::Base
  belongs_to :association
  has_many :owners, :dependent => :destroy
  accepts_nested_attributes_for :owners, :allow_destroy => true
end

owner.rb

class Owner < ActiveRecord::Base
  belongs_to :condominium
  belongs_to :user
end

When I try to delete an Association model my rails server just crashes.
Rails version 3.1.0 . And Ruby 1.9.2 p290
Any ideas why is this happening ?

Thanks.

UPDATE: If I try to remove the :dependent => :destroy it works. But because I need to create a batch of owners when a Condominium object is build I added:

def new_owners
    return 0
  end

  def new_owners=(int_num)
    int_num = int_num.to_i
    if int_num > 0
      int_num.times do
        self.owners.create
      end
    end
  end

The result is the same. Rails server crashing on save.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救星 2024-12-13 20:44:33

在没有看到堆栈跟踪的情况下...小心使用像 Association 这样的名称。

在 ActiveRecord 中已经有一个 Association 类
https://github.com/rails/rails /blob/master/activerecord/lib/active_record/associations/association.rb

Without seeing the stack trace... beware of using names like Association.

In ActiveRecord there's already an Association class
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/association.rb

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文