{"id":270,"date":"2022-08-30T14:58:33","date_gmt":"2022-08-30T14:58:33","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/08\/31\/undefined-method-metadata-when-uploading-using-mongoid-paperclip-gem-record-and-share-programming-errors\/"},"modified":"2022-08-30T14:58:33","modified_gmt":"2022-08-30T14:58:33","slug":"undefined-method-metadata-when-uploading-using-mongoid-paperclip-gem-record-and-share-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/undefined-method-metadata-when-uploading-using-mongoid-paperclip-gem-record-and-share-programming-errors\/","title":{"rendered":"Undefined Method &#39;metadata&#39; when uploading using mongoid-paperclip gem-Record and share programming errors"},"content":{"rendered":"<p>As I said above, I had a similar problem with Mongoid, <strong>Carrierwave<\/strong> and GridFS. My solution is super hacky but it worked for me.<\/p>\n<p>I had an Image class which is where my image was mounted<\/p>\n<pre><code>class Image\n  include Mongoid::Document\n  include Mongoid::Timestamps\n\n  field :title\n  field :image\n  field :order\n\n  mount_uploader :image, ImageUploader\n  embedded_in :article\nend\n\nclass Article\n  include Mongoid::Document\n  ...\n  embeds_one :image\n  ...\nend\n<\/code><\/pre>\n<p>My carrierwave uploader wanted the attributes sent to it with the key of the mount uploader (image).<\/p>\n<pre><code>Image.create( :image =&gt; image_attributes)\n<\/code><\/pre>\n<p>But the article&#8217;s new\/edit form created something that looked like:<\/p>\n<pre><code>:article =&gt; { :image =&gt; #ActionDispatch... }\n<\/code><\/pre>\n<p>instead of<\/p>\n<pre><code>:article =&gt; { :image =&gt; { :image =&gt; #ActionDispatch... } }\n<\/code><\/pre>\n<p>so my solution was to change the name of the field in the form to<\/p>\n<pre><code>file_field :article, :photo\n<\/code><\/pre>\n<p>and then add a photo setter to the article class that created an image<\/p>\n<pre><code>model Article\n  include Mongoid::Document\n  ...\n  def photo=(attrs)\n    create_image(:image =&gt; attrs)\n  end\nend\n<\/code><\/pre>\n<p>I tried this with image= but it infinitely recursed and did evil things.<br \/>\nI also tried this<\/p>\n<pre><code>file_field \"article[image]\", :image\n<\/code><\/pre>\n<p>without the setter and it didn&#8217;t raise an exception but it also didn&#8217;t save my image.<\/p>\n<p>as far as i know, paperclip is pretty similar in these regards. Maybe this will work for someone or someone can clean up my mess<\/p>\n<p id=\"rop\"><small>Originally posted 2013-08-31 07:54:49. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>As I said above, I had a similar problem with Mongoid, Carrierwave and GridFS. My solution is super hacky but it worked for me. I had an Image class which is where my image was mounted class Image include Mongoid::Document include Mongoid::Timestamps field :title field :image field :order mount_uploader :image, ImageUploader embedded_in :article end class [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-270","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=270"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/270\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}