Validating uniqueness of nested data. Using cocoon gem

淺唱寂寞╮ 提交于 2020-01-06 20:01:52

问题


I currently have a Client model that has_many pricings.

Pricings table:

create_table "pricings", force: true do |t|
    t.integer  "product_id"
    t.integer  "client_id"
    t.decimal  "unit_price"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

Pricings model:

class Pricing < ActiveRecord::Base
  belongs_to :client
  belongs_to :product  
  validates :unit_price, presence: true  
end

Client model:

class Client < ActiveRecord::Base
  has_many :deliveries
  has_many :collections
  has_many :pricings
  accepts_nested_attributes_for :pricings, allow_destroy: true

  scope :order_by_name, -> { order('lower(name)') }

  validates :name, :address, :vat_number, presence: true 
  validates :pricings,  presence:  { :message => ": Products must be added for a client before you can save." }
end

As you can see above, when I create,save,update a client the pricings should be present. What I want now is to make sure that the pricings have a unique product_id (no two pricings can have the same product_id.)

I'm making use of the cocoon gem ('cocoon', '~> 1.2.3' - https://github.com/nathanvda/cocoon) and found this article to help me out, but im really struggling to understand where I should add the code?

Link to code I don't understand: http://techbrownbags.wordpress.com/2014/02/05/rails-validation-of-cocoon-nested-forms/

How can I adapt that code to my situation?


回答1:


You need to add a validation to your Pricing model:

class Pricing < AR::Base

  validates :product_id, uniqueness: true

end

Note that even with this, there is still a chance that two duplicates will be entered in your database (especially when you use multithreaded server like unicorn or your app is running on multimple servers). To be 100% sure, you have to introduce db constraint. You can do this with simple migration:

add_index :pricings, :product_id, :unique => true  


来源:https://stackoverflow.com/questions/22948051/validating-uniqueness-of-nested-data-using-cocoon-gem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!