From 3704088ebde5ef074d186bff0d380858a9a01055 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 29 Aug 2006 17:06:27 +0000 Subject: has_one supports the :dependent => :delete option which skips the typical callback chain and deletes the associated object directly from the database. Closes #5927. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4848 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/fixtures/accounts.yml | 7 ++++++- activerecord/test/fixtures/companies.yml | 7 ++++++- activerecord/test/fixtures/company.rb | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) (limited to 'activerecord/test/fixtures') diff --git a/activerecord/test/fixtures/accounts.yml b/activerecord/test/fixtures/accounts.yml index a3d6742d79..b2d0191900 100644 --- a/activerecord/test/fixtures/accounts.yml +++ b/activerecord/test/fixtures/accounts.yml @@ -20,4 +20,9 @@ last_account: rails_core_account_2: id: 5 firm_id: 6 - credit_limit: 55 \ No newline at end of file + credit_limit: 55 + +odegy_account: + id: 6 + firm_id: 9 + credit_limit: 53 diff --git a/activerecord/test/fixtures/companies.yml b/activerecord/test/fixtures/companies.yml index f2e638d382..c61128c09b 100644 --- a/activerecord/test/fixtures/companies.yml +++ b/activerecord/test/fixtures/companies.yml @@ -47,4 +47,9 @@ leetsoft: jadedpixel: id: 8 name: Jadedpixel - client_of: 6 \ No newline at end of file + client_of: 6 + +odegy: + id: 9 + name: Odegy + type: ExclusivelyDependentFirm diff --git a/activerecord/test/fixtures/company.rb b/activerecord/test/fixtures/company.rb index 250deebb21..a55632360f 100755 --- a/activerecord/test/fixtures/company.rb +++ b/activerecord/test/fixtures/company.rb @@ -42,6 +42,9 @@ class DependentFirm < Company has_many :companies, :foreign_key => 'client_of', :order => "id", :dependent => :nullify end +class ExclusivelyDependentFirm < Company + has_one :account, :foreign_key => "firm_id", :dependent => :delete +end class Client < Company belongs_to :firm, :foreign_key => "client_of" @@ -83,6 +86,18 @@ end class Account < ActiveRecord::Base belongs_to :firm + def self.destroyed_account_ids + @destroyed_account_ids ||= Hash.new { |h,k| h[k] = [] } + end + + before_destroy do |account| + if account.firm + Account.destroyed_account_ids[account.firm.id] << account.id + end + true + end + + protected def validate errors.add_on_empty "credit_limit" -- cgit v1.2.3