Skip to content

Instantly share code, notes, and snippets.

@meinac
Created March 16, 2016 03:34
Show Gist options
  • Save meinac/fef74b76a04989f9927d to your computer and use it in GitHub Desktop.
Save meinac/fef74b76a04989f9927d to your computer and use it in GitHub Desktop.
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(false) do
source 'https://rubygems.org'
gem 'activerecord', '4.2.6'
gem 'sqlite3'
gem 'pry'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
require 'pry'
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :contents, force: true do |t|
t.integer :content_id, index: true
t.string :body
t.string :type
end
end
class Content < ActiveRecord::Base; end
class Post < Content
has_many :comments, foreign_key: :content_id
has_many :sub_comments, through: :comments
end
class Comment < Content
belongs_to :post, foreign_key: :content_id
has_many :sub_comments, foreign_key: :content_id
end
class SubComment < Content
belongs_to :comment, foreign_key: :content_id
has_one :post, through: :comment
end
class BugTest < Minitest::Test
def test_association_stuff
post = Post.create(body: 'post')
comment = Comment.create(body: 'comment', post: post)
sub_comment = SubComment.create(body: 'sub comment', comment: comment)
assert_equal post.sub_comments.first, sub_comment
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment