Skip to content

Instantly share code, notes, and snippets.

@kuldeepaggarwal
Last active August 29, 2015 14:06
Show Gist options
  • Save kuldeepaggarwal/cd9359b21c7c416dcbac to your computer and use it in GitHub Desktop.
Save kuldeepaggarwal/cd9359b21c7c416dcbac to your computer and use it in GitHub Desktop.
Has Many through, dependent: :destroy
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'arel', github: 'rails/arel', branch: 'master'
gem 'sqlite3'
gem 'minitest'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :themes do |t|
end
create_table :templates do |t|
t.references :theme
end
create_table :page_contents do |t|
t.references :component, polymorphic: true
t.references :container, polymorphic: true
end
create_table :placeholders do |t|
end
end
class Theme < ActiveRecord::Base
has_many :templates, dependent: :destroy
end
class Template < ActiveRecord::Base
belongs_to :theme
has_many :page_contents, as: :container, dependent: :destroy
has_many :placeholders, through: :page_contents, source: :component, source_type: 'Placeholder'
end
class PageContent < ActiveRecord::Base
# If we uncomment the dependent option, then everything works fine.
belongs_to :component, polymorphic: true#, dependent: :destroy
belongs_to :container, polymorphic: true
end
class Placeholder < ActiveRecord::Base
has_one :page_component, as: :component, class_name: 'PageContent'
has_one :page_container, as: :container, class_name: 'PageContent'
end
class BugTest < Minitest::Test
def test_association_stuff
theme = Theme.create!
template = theme.templates.create!
placeholder = template.placeholders.create!
theme.reload
template.reload
placeholder.reload
theme.destroy
assert_equal 0, Placeholder.count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment