contentful / contentful_model

A lightweight wrapper around the Contentful api gem, to make it behave more like ActiveRecord
MIT License
44 stars 42 forks source link

Getting errors when publishing #116

Closed pezholio closed 6 years ago

pezholio commented 6 years ago

When I try to publish a piece of content from the preview API, I get alternating errors:

ContentfulModel.use_preview_api = true
comment = Comment.find_by(id: params[:id]).load.first
ContentfulModel.use_preview_api = false
comment.publish
#=> undefined method `version=' for nil:NilClass
comment.publish
#=> undefined method `name' for nil:NilClass

The request that gets logged is as follows:

{:request=>{:url=>"https://cdn.contentful.com/spaces/7swdj0fkojyi/environments/test/entries", :query=>{:"sys.id"=>"3fgQjb9uekKeuw6EYQiucU"}, :header=>{"X-Contentful-User-Agent"=>"sdk contentful.rb/2.8.0; integration contentful_model/1.0.0; platform ruby/2.5.0; os macOS/17;", "Authorization"=>"Bearer 79f8543d1515fd5e50fb432729237fe799678a1cacc5006a062e72dd5172f219", "Content-Type"=>"application/vnd.contentful.delivery.v1+json", "Accept-Encoding"=>"gzip"}}}

Any ideas where I'm going wrong?

pezholio commented 6 years ago

This is still an issue. It seems to be occurring randomly. Users have reported it to me, but when I've gone into the console and tried it myself it works.

dlitvakb commented 6 years ago

Hey @pezholio,

Try doing the following:

comment.to_management.publish

It should not be required, but in some very edge cases, it may incur in a race condition. Using this method should partially mitigate that race condition, though it doesn't ensure it, as the Manageable module which exposes the publish method is already doing it under the hood. But manually triggering it may affect the ordering in which it happens, making it more predictable in some cases.

Hope this helps,

Cheers

pezholio commented 6 years ago

Cool. I'll give it a try. Thanks!