I have an array of objects, type Lab, existing_labs, and a different array of Labs, new_labs. I have defined my own == comparator for Lab.
existing_labs is Labs that already exist in the db.
new_labs is Labs to be saved, based on their existence in the db already.
Right now I am doing this
new_labs.each do |l|
lab = Lab.new(l)
exists = existing_labs.map{ |existing_lab| lab == existing_lab }
lab.save unless exists.include? true
end
This works, but can be slow if either or both of the arrays is large. I’m sure there has to be a better way to do this!
Edit for clarity:
My question is: Is there a good way to return all of the Lab that are present in new_labs that are not present in existing_labs, using my == method?
Should be somewhat faster, because no array of trues and falses is created for each lab. Also,
any?quits when onetrueis found.