codenoble / cache-crispies

Speedy Rails JSON serialization with built-in caching
MIT License
156 stars 14 forks source link

rendering outside controller #25

Closed chmich closed 4 years ago

chmich commented 4 years ago

Hi,

CerealSerializer.new(Cereal.first, be_trendy: true).as_json works,

but CerealSerializer.new(Cereal.first(3), be_trendy: true).as_json says: undefined method 'name' for #<Array:0x00007f8e2729b988> (name is attribute from my Customer)

regards, chris

adamcrown commented 4 years ago

Cache Crispies depends on the Rails #cache_key method. That method is available on an ActiveRecord::Relation but not on a simple Array.

For instance

Cereal.limit(3).cache_key
# => "stories/query-09849c8d1b8b5162808240504a4f1105-3-20190810184156000000"

Story.first(3).cache_key
# => NoMethodError (undefined method `cache_key' for #<Array:0x00007fa2728baf58>)

The same is true for the #name method, it's available on an ActiveRecord::Relation but not on Array. So you'll need to make sure you don't pass Arrays as arguments if you want the benefits of caching. In this case, Cereal.limit instead of Cereal.first should do the trick.