Skip to content

Instantly share code, notes, and snippets.

@matthewd
Created June 23, 2015 22:14
Show Gist options
  • Save matthewd/8b88b7356d4076fe6a10 to your computer and use it in GitHub Desktop.
Save matthewd/8b88b7356d4076fe6a10 to your computer and use it in GitHub Desktop.
require 'active_record'
require 'minitest/autorun'
require 'logger'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
end
create_table :comments, force: true do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
end
# 1. Add an association to the child class
class FancyPost < Post
belongs_to :author
end
# 2. Add an association to the parent, which the child should inherit
Post.class_eval do
has_many :comments
end
class Comment < ActiveRecord::Base
end
class BugTest < Minitest::Test
def test_association_stuff
post = FancyPost.new
# 3. Access the association --> ActiveRecord::AssociationNotFoundError
post.comments << Comment.new
post.save!
assert_equal 1, Comment.count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment