Skip to content

Instantly share code, notes, and snippets.

@nbudin
Created May 12, 2014 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbudin/c5fa13c3963514ec4ef9 to your computer and use it in GitHub Desktop.
Save nbudin/c5fa13c3963514ec4ef9 to your computer and use it in GitHub Desktop.
Test case for ActiveRecord filter_binds bug
# 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