我有两个ActiveRecord类。这些类的简化图:防止has_many集合中最后一条记录被删除的最佳方法是什么?
class Account < ActiveRecord::Base
has_many :user_account_roles
end
class UserAccountRole < ActiveRecord::Base
belongs_to :account
# Has a boolean attribute called 'administrator'.
end
我正在挣扎的是,我希望能够在两个验证规则适用于本: *确保最后UserAccountRole不能删除。 *确保无法删除管理员的最后一个UserAccountRole。
我真的很难理解实现这种结构验证的最佳方式。我尝试添加一个before_remove回调到关联,但我不喜欢这必须抛出一个错误,这将需要被控制器捕获。我宁愿把它当作“只是另一种验证”。
class Account < ActiveRecord::Base
has_many :user_account_roles, :before_remove => check_remove_role_ok
def check_remove_relationship_ok(relationship)
if self.user_account_relationships.size == 1
errors[:base] << "Cannot remove the last user from this account."
raise RuntimeError, "Cannot remove the last user from this account."
end
end
end
我不认为这什么差别,但我还使用accepts_nested_attributes_for。
好吧,不能相信这就是这么简单!认为Rails不够智能,无法自动管理它。我确实必须将验证设置为只在更新而不是创建时触发,否则实际上首先创建关系变得棘手。除此之外,似乎工作正常:)谢谢! – 2010-11-11 07:10:28
P.S.必须按照如下方式调整'at_least_one_user_account_role',以使验证规则在保存记录之前在验证中发现:if user_account_relationships.size <1 || user_account_relationships.all? {| r | r.marked_for_destruction?} – 2010-11-11 21:57:46