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

Contentful::Link rather than Contentful::Asset Cached object #110

Closed niquepa closed 6 years ago

niquepa commented 6 years ago

I have the following code to get some entries from contentful:

@professionals = Web::Dentist.all.params({'order' => 'fields.orden', 'locale' => I18n.locale}).load.items

each entry has a relation to an asset (picture), so I'm accessing it like:

- professionals.each do |dentist|
  = dentist.picture.url

It works without any problem and if you inspect each picture you'll see:

<Web::Dentist[dentist] id='496kcSEqCkAuaGiC8as2a8'>
[<Contentful::Asset id='51sOKzSr7iCAoygq6206ES' url='//images.ctfassets.net/0lo6jhr35aeg/51sOKzSr7iCAoygq6206ES/c4c73b51ed40248e9d5f8b2d3bdcab85/DR.Bedoya-web.jpg'>]

but if I try to cache the response like this:

@professionals = Rails.cache.fetch(:"professionals_#{I18n.locale}", expires_in: ENV['cache_ttl'].to_i) do
  Web::Dentist.all.params({'order' => 'fields.orden', 'locale' => I18n.locale}).load.items
end

and then iterate the entries, only the first item has the relation as Contentful::Asset, the rest of items have Contenful::Link, if you inspect each entry you'll see:

<Web::Dentist[dentist] id='496kcSEqCkAuaGiC8as2a8'>
[<Contentful::Link id='51sOKzSr7iCAoygq6206ES'>]

So it throws an error, again it only happens when you cache the response, I'm using memcached for it.

niquepa commented 6 years ago

I have the same Issue reported in contentful.rb https://github.com/contentful/contentful.rb/issues/138

have this issue currently but it only appears when we put a cache on an object. When I receive this object from contentful directly I get an Asset when it's a cache hit I sometimes get an Asset but some other time I get a Link. This is quite apparent because out of a collection of two things one has an image of type Asset while the second has an image of type Link.

dlitvakb commented 6 years ago

Hi @niquepa,

Can you let me know which version of the integration and SDK you are using?

This issue has been fixed almost a year ago. The contentful_model integration has been recently updated to version 1.x which includes all the new updates to the SDKs.

Cheers

niquepa commented 6 years ago

Hi @dlitvakb I'm including master branch:

gem 'contentful_model', :git => 'https://github.com/contentful/contentful_model.git', :branch => 'master'

checking the Gemfile.lock seems like I'm using v0.2.0:

  remote: https://github.com/contentful/contentful_model.git
  revision: 660bf1bbd499387a04c25e1f6fdbf8f0000c2beb
  branch: master
  specs:
    contentful_model (0.2.0)
      activesupport
      contentful (~> 2.0)
      contentful-management (~> 1.0)
      redcarpet

Should I use v1.0.0 ?

dlitvakb commented 6 years ago

Hey @niquepa,

Please try updating to use 1.0.0, as the dependencies listed seem outdated.

Cheers

niquepa commented 6 years ago

This is how my Gemfile.lock looks now:

GIT
  remote: https://github.com/contentful/contentful_model.git
  revision: 91b89361d85c310c42eefbf3c08cbbf372dcd3ad
  specs:
    contentful_model (1.0.0)
      activesupport
      contentful (~> 2.7)
      contentful-management (~> 2.0)
      redcarpet

but the error stills present. The way I get the content from contentful is:

    @professionals = Rails.cache.fetch(:"professionals_#{I18n.locale}", expires_in: ENV['cache_ttl'].to_i) do
      Web::Dentist.all.params({'order' => 'fields.orden', 'locale' => I18n.locale}).load.items
    end

Web::Dentist class is just 3 lines of code:

class Web::Dentist < ContentfulModel::Base
  self.content_type_id = "dentist"
end

The weird thing is that the first element always has the proper relation as ContentfulModel::Asset but the other elements have ContentfulModel::Link

          - professionals.each do |dentist|
            .item
              .thumbnail
                - if !dentist.fotos.blank? && dentist.fotos.first
                  %img{:alt => "", :src => dentist.fotos.first.try(:url)}/

As you can see in the below images, that happens for all the relations, the specialty is not loaded either:

contentful2 contentful1

Thanks for your help

dlitvakb commented 6 years ago

Hey @niquepa,

Can you create a support request here https://support.contentful.com requesting for me and provide me your Space ID and CDA Token? This looks like a possible bug, but I cannot know for sure unless I test it further.

Cheers

niquepa commented 6 years ago

@dlitvakb Done:

Ticket 27777

thanks.