Created
May 12, 2014 21:20
-
-
Save nbudin/c5fa13c3963514ec4ef9 to your computer and use it in GitHub Desktop.
Test case for ActiveRecord filter_binds bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Activate the gem you are reporting the issue against. | |
gem 'activerecord', '4.1.1' | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
# Ensure backward compatibility with Minitest 4 | |
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'test_ar') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :posts do |t| | |
t.string :title | |
t.text :authors | |
t.timestamps | |
end | |
create_table :comments do |t| | |
t.string :comment_text | |
t.string :author | |
t.integer :parent_post_id | |
t.timestamps | |
end | |
end | |
class Post < ActiveRecord::Base | |
# Changing the foreign_key parameter from a symbol to a string makes this test pass! | |
has_many :comments, foreign_key: :parent_post_id | |
before_save do |post| | |
post.authors = post.comments.map(&:author).to_sentence | |
end | |
end | |
class Comment < ActiveRecord::Base | |
belongs_to :post, foreign_key: :parent_post_id | |
after_save do |comment| | |
comment.post.updated_at = Time.now | |
comment.post.save(validate: false) | |
end | |
end | |
class BugTest < Minitest::Test | |
def test_creating_comments | |
post = Post.create!(title: "First Post") | |
post.comments.where(comment_text: "First comment!").first_or_create | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment