Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kaorukobo/fc3f74c708f8e3cf5e1cf34ee01caab1 to your computer and use it in GitHub Desktop.
Save kaorukobo/fc3f74c708f8e3cf5e1cf34ee01caab1 to your computer and use it in GitHub Desktop.
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: "main" # commit: 608c1bf
gem "sqlite3"
end
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
ActiveRecord::Base.belongs_to_required_by_default = true
class Post < ActiveRecord::Base
# test fails:
has_many :comments, foreign_key: "post_id"
# test passes:
# has_many :comments
# also passes with `inverse_of` workaround:
# has_many :comments, foreign_key: "post_id", inverse_of: :post
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class BugTest < Minitest::Test
def test_association_stuff
post = Post.new
post.comments.build
assert post.valid?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment