Skip to content

Attachment in embedded model won't get saved #3

Description

@michalvalasek

Hey @DimaSamodurov!

Have you ever tried the attachment on an embedded model?

I have a Gallery model and a Photo model. The Gallery embeds_many :photos (Photo is embedded_in :gallery) and I have defined attachment :image, type: :image field on the Photo model.

class Gallery
  include Mongoid::Document    

  field :name, type: String

  embeds_many :photos
  accepts_nested_attributes_for :photos, allow_destroy: true
end

class Photo
  include Mongoid::Document

  attachment :image, type: :image

  embedded_in :gallery
end

In the view I'm using nested forms via Cocoon, so that I can add one or more Photos to the Gallery.

Now when I fill in the whole form (Gallery + 1 Photo) and save it, everything gets saved except the image attachment to the Photo model. This is what is in the 'galleries` collection in the DB after save:

{
    "_id" : ObjectId("55f03cae618ead3f6800000a"),
    "name" : "first gallery",
    "photos" : [ 
        {
            "_id" : ObjectId("55f04787618ead534b000002")
        }
    ]
}

As you can see the embedded Photo document has been created, but is missing the image_id property.

I checked what comes from the form to the controller - all the data is there (including the refile JSON with uploaded image data).

Any ideas how could this be solved?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions