Rails 3.1.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在没有看到堆栈跟踪的情况下...小心使用像 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