Last active
June 7, 2016 23:54
-
-
Save Edouard-chin/b7fe1c9ce8bc86a734aeaf6b5fdd4c1d to your computer and use it in GitHub Desktop.
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
begin | |
require 'bundler/inline' | |
rescue LoadError => e | |
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler' | |
raise e | |
end | |
gemfile(true) do | |
source 'https://rubygems.org' | |
gem 'activerecord', '4.2.1' | |
gem 'sqlite3' | |
gem 'byebug' | |
end | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :posts, force: true do |t| | |
t.string :author_name | |
t.string :status | |
end | |
create_table :authors, force: true do |t| | |
t.string :author_name | |
t.string :gender | |
end | |
create_table :comments, force: true do |t| | |
t.integer :post_id | |
t.integer :author_id | |
t.string :comment | |
t.string :title | |
end | |
end | |
class Post < ActiveRecord::Base | |
validates :status, presence: true | |
validate :validate_associated_records_for_comments | |
validates :author_name, presence: true | |
has_many :comments, inverse_of: :post, autosave: true | |
accepts_nested_attributes_for :comments | |
end | |
class Author < ActiveRecord::Base | |
has_many :comments, inverse_of: :author, autosave: true | |
accepts_nested_attributes_for :comments | |
validates :gender, presence: true | |
validate :validate_associated_records_for_comments | |
validates :author_name, presence: true | |
end | |
class Comment < ActiveRecord::Base | |
belongs_to :post | |
belongs_to :author | |
validates :comment, :title, presence: true | |
end | |
class BugTest < Minitest::Test | |
def test_error_order_when_validation_is_declared_before_association | |
post = Post.new | |
post.author_name = '' | |
post.comments_attributes = { | |
"0" => { | |
comment: '', | |
title: '' | |
} | |
} | |
refute post.valid? | |
assert_equal :author_name, post.errors.keys.second | |
end | |
def test_error_order_when_validation_is_declared_after_association | |
author = Author.new | |
author.author_name = '' | |
author.comments_attributes = { | |
"0" => { | |
comment: '', | |
title: '' | |
} | |
} | |
refute author.valid? | |
assert_equal :'comments.comment', author.errors.keys.second | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment