jeudi 31 mai 2018

Active record expression to update values from another source?

I have the following data sources:

@current_products = Product.where(store_id: @current_store) # local db
@new_products = NewProduct.where(store_id: @new_store) # remote db

Customers update product_name within @new_products and I need those names to be updated within @current_products. A product will have a product_id that is the same on both @current_products and @new_products that I can use as a key.

I am starting off with:

product_ids = []
@current_products.each do |item|   
        product_ids << item[:product_id]
end  

but then I'm not sure how update the respective product_title within @current_products:

@new_products.each do |item|               
# code to update Product with respective product_id from NewProduct    

end

Should I be updating using something like the following?

product = Product.find_or_create_by(<something here>)
product.update_attributes(<something here>)

I'm trying to get my head around: https://apidock.com/rails/ActiveRecord/Base/update_all/class but I cannot find a relevant example for my case.

Aucun commentaire:

Enregistrer un commentaire