代码之家  ›  专栏  ›  技术社区  ›  Ronan Lopes

活动记录-重复的属性值,即使有验证

  •  0
  • Ronan Lopes  · 技术社区  · 6 年前

    我有一个基于作用域的唯一性验证模型(我使用的是Rails 5):

    validates_uniqueness_of :n_pacote, scope: :ecg_id
    

    即使有了验证,我也注意到 n_pacote 相同的值 ecg_id .我还注意到它们几乎是同时创建的:

    [Thu, 05 Jul 2018 14:07:59 -03 -03:00, Thu, 05 Jul 2018 14:07:58 -03 -03:00, Thu, 05 Jul 2018 14:07:58 -03 -03:00]
    

    它们是通过可能多次调用的API调用创建的。我怎样才能解决这个问题?提前谢谢

    编辑: 以下是3条记录的所有属性 帕科特 相同的价值 心电图 以下内容:

    {"id"=>32289, "ecg_id"=>29, "leitura"=>[170, 69, 85, 81, 69, 85, 81, 69, 85, 81, 70, 102, 98, 70, 102, 98, 70, 102, 98, 71, 119, 115, 71, 119, 115, 71, 119, 115, 72, 136, 132, 72, 136, 132, 72, 136, 132, 73, 153, 149, 73, 153, 149, 73, 153, 149], "posprocessada"=>nil, "created_at"=>Thu, 05 Jul 2018 14:07:59 -03 -03:00, "updated_at"=>Thu, 05 Jul 2018 14:07:59 -03 -03:00, "n_pacote"=>1620}
    {"id"=>32276, "ecg_id"=>29, "leitura"=>[170, 69, 85, 81, 69, 85, 81, 69, 85, 81, 70, 102, 98, 70, 102, 98, 70, 102, 98, 71, 119, 115, 71, 119, 115, 71, 119, 115, 72, 136, 132, 72, 136, 132, 72, 136, 132, 73, 153, 149, 73, 153, 149, 73, 153, 149], "posprocessada"=>nil, "created_at"=>Thu, 05 Jul 2018 14:07:58 -03 -03:00, "updated_at"=>Thu, 05 Jul 2018 14:07:58 -03 -03:00, "n_pacote"=>1620}
    {"id"=>32256, "ecg_id"=>29, "leitura"=>[170, 69, 85, 81, 69, 85, 81, 69, 85, 81, 70, 102, 98, 70, 102, 98, 70, 102, 98, 71, 119, 115, 71, 119, 115, 71, 119, 115, 72, 136, 132, 72, 136, 132, 72, 136, 132, 73, 153, 149, 73, 153, 149, 73, 153, 149], "posprocessada"=>nil, "created_at"=>Thu, 05 Jul 2018 14:07:58 -03 -03:00, "updated_at"=>Thu, 05 Jul 2018 14:07:58 -03 -03:00, "n_pacote"=>1620}
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   sgk    6 年前

    add_index :table_name, [:n_pacote, :ecg_id], unique: true