Last active
August 29, 2015 13:57
-
-
Save tanelj/9517608 to your computer and use it in GitHub Desktop.
Rails 4.1.0.rc1 has_and_belongs_to_many vaidation ignoring error.
This file contains hidden or 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
# gem 'activerecord', '4.0.3' | |
gem 'activerecord', '4.1.0.rc1' | |
# gem 'rails', github: 'rails/rails', branch: '4-1-stable' | |
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: 'sqlite3', database: ':memory:') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :events do |t| | |
end | |
create_table :languages do |t| | |
t.string :info | |
end | |
create_table :events_languages, id: false do |t| | |
t.integer :event_id | |
t.integer :language_id | |
end | |
end | |
class Language < ActiveRecord::Base | |
before_validation :run_before_create, on: :create | |
before_validation :run_before_validation | |
private | |
def run_before_create | |
write_attribute(:info, info.to_s + 'run_before_create') | |
end | |
def run_before_validation | |
write_attribute(:info, info.to_s + 'run_before_validation') | |
end | |
end | |
class Event < ActiveRecord::Base | |
has_and_belongs_to_many :languages, validate: false | |
end | |
class BugTest < Minitest::Test | |
def setup | |
language = Language.create! | |
language.update_column(:info, '') | |
end | |
def test_association_stuff | |
event = Event.new | |
language = Language.first | |
event.languages << language | |
event.valid? | |
assert_equal 1, event.languages.size | |
# Should not run associated language validation when :validate => false | |
assert_equal language.info, '' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment