I am trying to validate the uniqueness of the data i enter so there is no duplicates at all.
Model
class Asset < ActiveRecord::Base
validates :Devtype, presence: true
validates_uniqueness_of :Nic1mac, :scope => [:Nic1mac, :Nic2mac, :Nic3mac, :IBmac, :SN], :allow_blank => true
validates_uniqueness_of :Nic2mac, :scope => [:Nic1mac, :Nic2mac, :Nic3mac, :IBmac, :SN], :allow_blank => true
validates_uniqueness_of :Nic3mac, :scope => [:Nic1mac, :Nic2mac, :Nic3mac, :IBmac, :SN], :allow_blank => true
validates_uniqueness_of :IBmac, :scope => [:Nic1mac, :Nic2mac, :Nic3mac, :IBmac, :SN], :allow_blank => true
validates_uniqueness_of :SN, :scope => [:Nic1mac, :Nic2mac, :Nic3mac, :IBmac, :SN], :allow_blank => true
end
This is what I saw recommended in similar questions but it is not working for me. I input my first data in the "Nic1mac" field. When I create another entry and try to populate the "Nic1mac" field with the same data it stops me as is expected. But when i populate the "Nic2mac" field with the same data that is in the "Nic1mac" field it allows for the record to be created without any error. Thus I have a duplicate mac address in different fields. Another issue is if i create a new record and enter the same data in the "Nic1mac" and the "Nic2mac" it allows the record to be created without noticing that the data in the "Nic1mac" is the same in another record.
Example
Record one-
Nic1mac = 1234
Record two-
Nic2mac = 1234 (no error is shown and record is allowed to be created even though the data is the same as in record 1 Nic1mac)
Record three-
Nic1mac = 1234 (error comes up as expected)
Nic2mac = 1234 (allows for record creation even though data is the same as record 1 and 2)
I would also like to prevent duplicate data from being entered at the record creation step.
Example
Record one creation-
Nic1mac = 1234
Nic2mac = 1234
I want an error to arise that does not allow me to create the record because of the duplicate data being inputed into two different fields. If more clarity is needed or and more information or code please let me know and I will do my best to provide it.
Aucun commentaire:
Enregistrer un commentaire