Rabl caching with a nested object

Here are my rabl views:

Students /show.json.rabl

object @student cache @student attribute :name, :gender, :age node :school do |student| partial("shared/school", :object => student.school) end 

general /_school.json.rabl

 object @school cache @school attributes :id, :name 

student.rb:

 class Student < ActiveRecord::Base belongs_to :school, :touch => true end 

school.rb

 class School < ActiveRecord::Base has_many :students end 

Therefore, when I update a student, the cache becomes invalid as expected. The problem is that when I update the school, the students do not receive the updated school attributes. I would have thought that the @school cache would partially become invalid when I update the school model, but that doesn't seem to be the case.

EDIT: Added touch on student to cancel school on upgrade.

+4
source share
3 answers

In the interest of helping someone else, here's what I'm doing now:

I broke the presentation of the student show into:

Students /show.json.rabl:

 extends "students/min_show" node :school do |student| partial("shared/school", :object => student.school) end 

and students / min _show.json

 object @student cache @student attribute :name, :gender, :age 

Thus, I still read the cache for the student, but the school is always displayed from the template and cached separately.

+1
source

To update School caching School , you must update Student :

class Student <ActiveRecord :: Base

 belongs_to :school, :touch => true 

end

+1
source

I left a comment on your issue, where I recommend putting students in school. An easy workaround that should work for you:

 cache [@student, @student.school] 

Thus, it checks cache_key on both objects.

0
source

All Articles