Skip to content

Instantly share code, notes, and snippets.

@nbudin
Created May 12, 2014 21:53
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/24b44fb127df86d46ca7 to your computer and use it in GitHub Desktop.
Save nbudin/24b44fb127df86d46ca7 to your computer and use it in GitHub Desktop.
Test case for ActiveRecord filter_binds issue on Rails master
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'pg'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
# 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