Skip to content

Instantly share code, notes, and snippets.

@delsoft
Last active January 4, 2016 17:29
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 delsoft/8654369 to your computer and use it in GitHub Desktop.
Save delsoft/8654369 to your computer and use it in GitHub Desktop.
rails active record issue
require "rubygems"
gem "minitest"
gem 'debugger'
# Activate the gem you are reporting the issue against.
gem 'activerecord', '3.2.15'
require 'active_record'
require 'minitest/autorun'
require 'logger'
require 'debugger'
# 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: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts do |t|
t.string :title
t.text :article
end
create_table :comments do |t|
t.integer :post_id
t.text :comment
end
end
class Post < ActiveRecord::Base
has_many :comments, autosave: :true
validates :article, presence: :true, on: :my_context
end
class Comment < ActiveRecord::Base
belongs_to :post
validates :comment, presence: :true, on: :my_context
end
class BugTest < Minitest::Test
def test_association_stuff
post = Post.new
post.comments << comment = Comment.new
assert_equal post.valid?, true
assert_equal post.valid?(:my_context), false
assert_includes post.errors.messages, :article
# mistakes start here
assert_includes post.comments[0].errors.messages, :comment, "not validated comment when validate in context"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment