sirupsen / airrecord

Ruby wrapper for Airtable, your personal database

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not updating filenames in attachment

jjercx opened this issue · comments

Hi, first time using Airrecord, not sure if this is a problem or I'm missing something. I'm trying to update filenames from records. I'm changing the record and then using save, the response is true, yet, I don't see the records changed in Airtable. Even if I run the program again, while reading again the record, it has the old name.

Is filename a calculated field you can't update? Haven't found something like that on the docs. Thanks!

Airrecord.api_key = ENV['AIRTABLE_API_KEY']

class Product < Airrecord::Table
  self.base_key = ENV['AIRTABLE_BASE_ID']
  self.table_name = 'Producto'
end

prod = Product.all.first
p prod["Image"].first["filename"]  
# => 'old_name.jpg'
prod["Image"].first["filename"] = 'new_name.jpg'
p prod["Image"].first["filename"]  
# => 'new_name.jpg'
prod.save
# => true

I suspect your second .first is giving you a copy, not actually modifying the array. Print out the whole object before saving to check for me?

I printed and shows properly:

[2] pry(main)> prod
=> #<Product:0x00007f878bb09810
 @created_at=2020-12-15 18:04:03 UTC,
 @fields=
  {"Name"=>"Hombres Lobo de Castronegro",
   "Tipo"=>"Juego de mesa",
   "Inventario"=>["receTIYp4W8Ncgdjw"],
   "Producto Compra"=>["rec4Lf4V5AR0gUuM3"],
   "Precio de venta"=>50,
   "Image"=>
    [{"id"=>"attEpBKQIxgJcNgVb",
      "url"=>"https://dl.airtable.com/.attachments/58469c52ee6286e4b46391f5a2c40972/c2ffc326/los-hombres-lobo-de-castronegro.jpg",
      "filename"=>"los-hombres-lobo-de-castronegrooooo.jpg", ### <====== HERE
      "size"=>46780,
      "type"=>"image/jpeg",
      "thumbnails"=>
       {"small"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/9f1e07905ec0984c70a885655c8a10a3/6ffdb6de", "width"=>32, "height"=>36},
        "large"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/5e2122609d2606c1043f5617a4d3a248/52b5be67", "width"=>512, "height"=>585},
        "full"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/925f79a975ebe5af1092fa32ef0f36ba/6ae3d3a2", "width"=>3000, "height"=>3000}}}],
   "Descripción"=>
    "Hombres lobo de Castronegro es un juego de suspense que usa un Moderador de juego para supervisar el enfrentamiento entre los hambrientos hombres lobo y los aldeanos que quieren eliminarlos. Al comienzo del juego el Moderador coge el número apropiado de cartas de personaje, las mezcla y le da una a cada jugador. Una vez que recibes tu carta, la miras en secreto y la colocas boca abajo frente a ti.\n",
   "Min Jugadores"=>8,
   "Max Jugadores"=>18,
   "Min Tiempo"=>1800,
   "Edad"=>10,
   "Complejidad"=>1,
[3] pry(main)> prod["Image"].first["filename"] = 'hola.jpg'
=> "hola.jpg"
[4] pry(main)> prod
=> #<Product:0x00007f878bb09810
 @created_at=2020-12-15 18:04:03 UTC,
 @fields=
  {"Name"=>"Hombres Lobo de Castronegro",
   "Tipo"=>"Juego de mesa",
   "Inventario"=>["receTIYp4W8Ncgdjw"],
   "Producto Compra"=>["rec4Lf4V5AR0gUuM3"],
   "Precio de venta"=>50,
   "Image"=>
    [{"id"=>"attEpBKQIxgJcNgVb",
      "url"=>"https://dl.airtable.com/.attachments/58469c52ee6286e4b46391f5a2c40972/c2ffc326/los-hombres-lobo-de-castronegro.jpg",
      "filename"=>"hola.jpg",  ### <====== HERE
      "size"=>46780,
      "type"=>"image/jpeg",
      "thumbnails"=>
       {"small"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/9f1e07905ec0984c70a885655c8a10a3/6ffdb6de", "width"=>32, "height"=>36},
        "large"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/5e2122609d2606c1043f5617a4d3a248/52b5be67", "width"=>512, "height"=>585},
        "full"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/925f79a975ebe5af1092fa32ef0f36ba/6ae3d3a2", "width"=>3000, "height"=>3000}}}],
   "Descripción"=>
    "Hombres lobo de Castronegro es un juego de suspense que usa un Moderador de juego para supervisar el enfrentamiento entre los hambrientos hombres lobo y los aldeanos que quieren eliminarlos. Al comienzo del juego el Moderador coge el número apropiado de cartas de personaje, las mezcla y le da una a cada jugador. Una vez que recibes tu carta, la miras en secreto y la colocas boca abajo frente a ti.\n",
   "Min Jugadores"=>8,
   "Max Jugadores"=>18,
   "Min Tiempo"=>1800,
   "Edad"=>10,
   "Complejidad"=>1,
[5] pry(main)> prod.save
=> true
[6] pry(main)> prod
=> #<Product:0x00007f878bb09810
 @created_at=2020-12-15 18:04:03 UTC,
 @fields=
  {"Name"=>"Hombres Lobo de Castronegro",
   "Tipo"=>"Juego de mesa",
   "Inventario"=>["receTIYp4W8Ncgdjw"],
   "Producto Compra"=>["rec4Lf4V5AR0gUuM3"],
   "Precio de venta"=>50,
   "Image"=>
    [{"id"=>"attEpBKQIxgJcNgVb",
      "url"=>"https://dl.airtable.com/.attachments/58469c52ee6286e4b46391f5a2c40972/c2ffc326/los-hombres-lobo-de-castronegro.jpg",
      "filename"=>"hola.jpg",
      "size"=>46780,
      "type"=>"image/jpeg",
      "thumbnails"=>
       {"small"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/9f1e07905ec0984c70a885655c8a10a3/6ffdb6de", "width"=>32, "height"=>36},
        "large"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/5e2122609d2606c1043f5617a4d3a248/52b5be67", "width"=>512, "height"=>585},
        "full"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/925f79a975ebe5af1092fa32ef0f36ba/6ae3d3a2", "width"=>3000, "height"=>3000}}}],
   "Descripción"=>
    "Hombres lobo de Castronegro es un juego de suspense que usa un Moderador de juego para supervisar el enfrentamiento entre los hambrientos hombres lobo y los aldeanos que quieren eliminarlos. Al comienzo del juego el Moderador coge el número apropiado de cartas de personaje, las mezcla y le da una a cada jugador. Una vez que recibes tu carta, la miras en secreto y la colocas boca abajo frente a ti.\n",
   "Min Jugadores"=>8,
   "Max Jugadores"=>18,
   "Min Tiempo"=>1800,
   "Edad"=>10,
   "Complejidad"=>1,

Ah ok. What type is the "Image" column in airtable? Attachment?

Someone else might need to chime in -- I'm not sure Airtable will let you modify the filename parameter after you've already uploaded a file. For new attachments, the url property is mandatory and the filename is optional, but afaik the filename property doesn't actually do anything (check the Airtable API documentation on your table for details).

(e.g. I think this is an Airtable limitation, not airrecord)

It looks you can't update an attachment's filename with the API. The API docs mention that you can delete an attachment by removing it from the array and you can upload a new attachment with a URL and filename. So as a workaround you can delete the original attachment and create a new one with the original URL and a new filename.

Note that this snippet works when there is only one attachment. If you have multiple attachments then you would need to modify the logic to ensure you don't delete all of the attachments.

prod = Product.all.first
image = prod["Image"].first
prod["Image"] = [{ "url" => image["url"], "filename" => "new_name.jpg" }]
prod.save

Ah ok. What type is the "Image" column in airtable? Attachment?

Yes, attachment.

(e.g. I think this is an Airtable limitation, not airrecord)

Seems reasonable, thanks! As a workaround, I'll try removing the image, and re-adding it modified.

check the Airtable API documentation on your table for details

I checked, as you said, it says url mandatory, filename optional. I think it refers to new uploads, and behaviour with editing metadata (i.e: filename) ain't specified. Haven't played directly with REST API though.

@rcscott works !! thanks for the clever snippet and for the fast replies ❤️

Should I close the issue? Should we document it somewhere?

Yeah I think it'd be nice to document under https://github.com/sirupsen/airrecord#file-uploads even though it's really an Airtable thing -- easy to get tripped up on.

Ok, I'll send a PR with the doc update soon :D

Just for the record, confirmed the issue was it Airtable API behaviour and not in this gem i.e. "Airtable REST API does NOT update filename if it's changed on the update request"

image