Skip to content

has_many :through **validate=>false** not being obeyed during association update #25718

@marcalc

Description

@marcalc

Steps to reproduce

Bug report script attached

Expected behavior

As per docs, when setting the option has_many :validate => false, the associated objects will not be validated when saving the object.

4.1.2.11 :validate

If you set the :validate option to true, then associated objects will be validated whenever you save this object. By default, this is false: associated objects will not be validated when this object is saved.

Actual behavior

When updating the association, using #role_ids=, the associated object is being validated.

System configuration

Rails version: 5.0.0

Ruby version: 2.3.1

https://gist.github.com/marcalc/a370674c040bd2122da729b904557c08#file-bug-report-rb


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'
  # Activate the gem you are reporting the issue against.
  gem 'activerecord', '5.0.0'
  gem 'sqlite3'
end

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 "roles", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "user_roles", force: :cascade do |t|
    t.integer  "user_id"
    t.integer  "role_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["role_id"], name: "index_user_roles_on_role_id"
    t.index ["user_id"], name: "index_user_roles_on_user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
end

class Role < ActiveRecord::Base
  validates_presence_of :name
end

class User < ActiveRecord::Base
  has_many :user_roles
  has_many :roles, through: :user_roles, validate: false
end

class UserRole < ActiveRecord::Base
  belongs_to :user
  belongs_to :role
end

class BugTest < Minitest::Test
  def test_association_stuff
    Role.new(name: nil).save(validate: false) # deliberately creating an invalid nameless role

    u = User.create!(role_ids: ["1"]) # OK
    u.update!(role_ids: nil) # OK
    u.update!(role_ids: ["1"]) # FAILS
  end
end

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions