RailsCasts Pro episodes are now free!

Learn more or hide this

sartip's Profile

GitHub User: sartip

Comments by

Avatar

In case it might help someone, I switch from creating a class in app/datatables to use Jbuilder.

This gave me the huge advantage to use cache. I tried delegating the cache method in my class but with no success...

Here is an example:

product_controller.rb
def index
  @products = Products.all.page(1).per(50)
end
products/index.json.jbuilder
json.sEcho params[:sEcho].to_i
json.iTotalRecords @products.total_count
json.iTotalDisplayRecords @products.total_count
json.set! :aaData do
  json.array! @products, partial: 'show', as: :product
end
products/_show.json.jbuilder
json.cache! [product] do
  json.id product.id
  json.name product.name
end

hope this helps (Im using kaminari)