Skip to content

Instantly share code, notes, and snippets.

@whalec
Created April 27, 2010 08:54
Show Gist options
  • Save whalec/380508 to your computer and use it in GitHub Desktop.
Save whalec/380508 to your computer and use it in GitHub Desktop.
class CreateMedia < ActiveRecord::Migration
def self.up
create_table :media do |t|
t.string :name, :null => false
t.text :description
t.references :asset, :null => false
t.timestamps
end
execute <<-EOS
ALTER TABLE media
ADD FOREIGN KEY (asset_id)
REFERENCES assets (id)
ON DELETE CASCADE
ON UPDATE CASCADE;
EOS
end
def self.down
drop_table :medias
end
end
# Delete ignores AR callbacks
it "should cascade delete the asset if deleted" do
asset = @media.asset
@media.delete
asset.reload.should be_nil
end
@xaviershay
Copy link

you're expecting the cascade to go the wrong way - media BELONGS TO asset. If you delete the asset, the media will be deleted. Since media is a child of asset, deleting media will not delete the asset.

@whalec
Copy link
Author

whalec commented Apr 27, 2010

Ahh right of course. Cool thanks I've got it now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment