私はシンプルなモデルを持っていUser
ます。Account
ユーザーが関連付けられているアカウントが削除されないようにしたい。User
私は一つ一つを創造Account
し、それらを関連付けます。次に、Account.find(x).destroy
コンソールで行います。アカウントが壊れる!
ノート:
- ユーザー
account_id
が正しいです。 Account.find(x).users.empty?
コンソールリターン時false
Account.find(x).destroyable?
コンソールリターン時true
users.empty?
def destroyable?
リターンでtrue
!!
私は何か間違ったことをしていますか?それは何ですか?
コード (Ruby 1.9.2-p290 上の Rails 3.2.9):
class User < ActiveRecord::Base
belongs_to :account
end
class Account < ActiveRecord::Base
has_many :users, dependent: :destroy
attr_accessible :name
before_destroy :destroyable?
def destroyable?
if users.empty? # This returns true when called via callback.
true
else
false
end
end
end