Trying to undestand new record?,changed ? and persisted ? on the example below.
How, this code has this result:
# NOW
# there is 0 new record after running new_data array
# there is 0 changed record(s) after running new_data array
# there is 3 persisted record(s) after running new_data array
Trying to come to this result:
# WANT
#there is 1 new record(s) after running new_data array
#there is 1 changed record(s) after running new_data array
#there is 2 persisted record(s) after running new_data array
*Question is: *
How to do it?. Want to record the number of changed and new records.
Cars.delete_all
original_data = [
{:brand => 'Mercedes', :used => false, :year => 2000 },
{:brand => 'Honda', :used => true , :year => 2000 },
{:brand => 'Nissan', :used => false, :year => 2000 }
]
new_data = [
# updating year to 2013
{:brand => 'Mercedes', :used => false, :year => 2013 },
# new record
{:brand => 'Tesla', :used => false, :year => 2013 },
# same data
{:brand => 'Nissan', :used => false, :year => 2000 }
]
# create new cars
original_data.each do |c|
Cars.create(c)
end
# updates
persisted = 0
new_records = 0
changed_records = 0
new_data.each do |c|
car = Cars.where(:brand => c[:brand]).first_or_create
persisted +=1 if car.persisted?
new_records +=1 if car.new_record?
changed_records +=1 if car.changed?
end
puts "there is #{new_records} new record(s) after running new_data array"
puts "there is #{changed_records} changed record(s) after running new_data array"
puts "there is #{persisted} persisted record(s) after running new_data array"
*UPDATE*
code according to answer: - attempt to change only 1 record, not 2.
# updates
persisted = 0
new_records = 0
changed_records = 0
new_data.each do |c|
car = Cars.where(:brand => c[:brand]).first_or_initialize
car.assign_attributes(c)
persisted +=1 if car.persisted?
new_records +=1 if car.new_record?
changed_records +=1 if car.changed?
car.save
end
there is 1 new record(s) after running new_data array
there is 2 changed record(s) after running new_data array
there is 2 persisted record(s) after running new_data array