Skip to content

Instantly share code, notes, and snippets.

@Envek
Created November 19, 2013 15:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Envek/7546920 to your computer and use it in GitHub Desktop.
Save Envek/7546920 to your computer and use it in GitHub Desktop.
Self-contained gist for Ruby on Rails bug report
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.0.1'
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 :polls do |t|
t.datetime :published_at # it's NULL by default!
t.integer :poll_answers_count, nullable: false, default: 0
t.timestamps
end
create_table :poll_answers do |t|
t.references :poll
end
end
class Poll < ActiveRecord::Base
has_many :poll_answers, inverse_of: :poll
# In most cases we want to deal only with published polls,
# admin panel controllers calls `unscoped` explicitly.
default_scope -> { where('polls.published_at <= ?', Time.now) }
end
class PollAnswer < ActiveRecord::Base
belongs_to :poll, inverse_of: :poll_answers, counter_cache: true
end
class BugTest < Minitest::Test
def test_association_stuff
poll = Poll.create!
poll.poll_answers.create!
assert_equal 1, poll.poll_answers.size
assert_equal 1, PollAnswer.count
assert_equal poll.id, PollAnswer.first.poll.id
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment