Rails 3 skip validations and callbacks

前端 未结 9 1702
野的像风
野的像风 2020-12-07 23:58

I have a particularly complex model with validations and callbacks defined. The business needs now calls for a particular scenario where adding a new record requires skippin

9条回答
  •  盖世英雄少女心
    2020-12-08 00:28

    I would recommend NOT using the skip_callback approach since it is not thread safe. The sneaky save gem however is since it just runs straight sql. Note this will not trigger validations so you will have to call them yourself (ex: my_model.valid?).

    Here are some samples from their docs:

    # Update. Returns true on success, false otherwise.
    existing_record.sneaky_save
    
    # Insert. Returns true on success, false otherwise.
    Model.new.sneaky_save
    
    # Raise exception on failure.
    record.sneaky_save!
    

提交回复
热议问题