Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save etiennebarrie/ca0b2f00537f6e0c0e110dcb843f2d08 to your computer and use it in GitHub Desktop.

Select an option

Save etiennebarrie/ca0b2f00537f6e0c0e110dcb843f2d08 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails", branch: 'master'
gem "sqlite3"
end
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 :posts, force: true do |t|
end
create_table :comments, force: true do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_many :comments,
before_add: :before_add
def before_add(comment)
raise "comment already in the association, should not be passed to before_add" if comment.persisted?
end
end
class Comment < ActiveRecord::Base
belongs_to :post, inverse_of: :comments
end
class BugTest < Minitest::Test
def test_association_stuff
Post.create!.comments.create!.reload.post
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment