using rails 4. calling api, returns shop , photos. when returns photos, 10 photos.
everytime call api, find_or_initialize_by
shop, update shop details. @ same time, want update photos. there no way know whether photos same or different, not url. different photo url same photos created previously. there's no reference id, , not saving/downloading photos in server too.
i have following code:
@shop = shop.find_or_initialize_by(source_id: shop_params[:id]) @shop.photos.build(shop_params[:photos])
question
let's when first creat shop a, api returns 4 photos. save 4 photos. call api again shop a, api returns 10 photos. want save these 10 photos. don't mind if previous 4 destroyed or updated. how make work? total photos should 10 after saving.
then, may call api again, returns 6 photos. don't want keep previous 10 photos, 6 photos.
in view, call @shop.photos
, latest photos (up 10).
note photos returned api duplicate ones have in db. that's reason why can't keep previous photos.
you can delete previous records , create new photos.
@shop = shop.find_or_initialize_by(source_id: shop_params[:id]) @shop.save_photos(shop_params[:photos])
in shop.rb
def save_photos(photos) self.photos.destroy_all self.photos.build(photos) end
it not robust solution work
Comments
Post a Comment