简体   繁体   中英

Why doesn't the “validate” option on HABTM work?

Using Rails 5.0.0.rc1 , I'm not able to get the "validate" option to work on a HABTM association. I thought the point of having "validate: false" is to tell rails not to validate the associated objects when the parent object is saved. So, that's my desired behavior - I don't want to validate the associated Parts when the Assembly is saved. What am I doing wrong? Thanks for any help!

EDIT: I tried this same example with rails 4.2.6 and I'm able to change the assembly's part_ids without getting the validation error. However, using the "validate: false" on the assembly model had no effect...it worked either way even though I had invalid part objects on the association.

Here's my example:

class Assembly < ApplicationRecord
  has_and_belongs_to_many :parts, validate: false
end

class Part < ApplicationRecord
  validates :name, presence: true
  validates :serial_number, presence: true
  has_and_belongs_to_many :assemblies
end

ActiveRecord::Schema.define(version: 20160608024352) do

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

  create_table "assemblies_parts", id: false, force: :cascade do |t|
    t.integer "assembly_id", null: false
    t.integer "part_id",     null: false
    t.index ["assembly_id"], name: "index_assemblies_parts_on_assembly_id"
    t.index ["part_id"], name: "index_assemblies_parts_on_part_id"
  end

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

end

I created some parts and assemblies in the database and then added the 2 validations to the Part model to recreate my problem (rails console follows):

Loading development environment (Rails 5.0.0.rc1)
irb(main):001:0> a = Assembly.first
Assembly Load (0.2ms)  SELECT  "assemblies".* FROM "assemblies" ORDER BY "assemblies"."id" ASC LIMIT ?  [["LIMIT", 1]]
=> #<Assembly id: 1, name: "stage 1", created_at: "2016-06-08 02:46:59", updated_at: "2016-06-08 02:46:59">
irb(main):002:0> a.part_ids
   (0.2ms)  SELECT "parts".id FROM "parts" INNER JOIN "assemblies_parts" ON "parts"."id" = "assemblies_parts"."part_id" WHERE "assemblies_parts"."assembly_id" = ?  [["assembly_id", 1]]
=> [1, 2]
irb(main):003:0> a.part_ids = [2,3]
  Part Load (0.3ms)  SELECT "parts".* FROM "parts" WHERE "parts"."id" IN (2, 3)
  Part Load (0.2ms)  SELECT "parts".* FROM "parts" INNER JOIN "assemblies_parts" ON "parts"."id" = "assemblies_parts"."part_id" WHERE "assemblies_parts"."assembly_id" = ?  [["assembly_id", 1]]
   (0.1ms)  begin transaction
  SQL (1.1ms)  DELETE FROM "assemblies_parts" WHERE "assemblies_parts"."assembly_id" = ? AND "assemblies_parts"."part_id" = 1  [["assembly_id", 1]]
   (3.1ms)  rollback transaction
ActiveRecord::RecordInvalid: Validation failed: Serial number can't be blank
    from /Users/lsmith/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0.rc1/lib/active_record/validations.rb:78:in `raise_validation_error'

:validate option only works on new records. If you try to add existing records it will always validate them. This behavior is different from Rails 4. There is a discussion regarding that in github - https://github.com/rails/rails/issues/25718

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM