Skip to content

Instantly share code, notes, and snippets.

@dnsilva
dnsilva / gist:3e24e4ad0519558b2529
Created July 2, 2015 07:07
Generate json for object with associations rails
def setup_custom_json_for_datagrid(shops)
json_data = shops.to_json(only: [:id, :name], include: { concept: { only: :name, include: { chain: { only: :name} } } })
jsonResponse = "{\"current\": #{params[:current]}, \"rowCount\": #{params[:rowCount]}, \"rows\": #{json_data}, \"total\": #{shops.length}}"
end
@shops = Shop.includes(concept: [:chain]).where(where_clause_map).order("#{attribute} #{ordering}").limit(params[:rowCount]).offset(offset)
Where:
Shop includes concept and in turn concept includes chain
and
where_clause_map looks like: {"id" => "[1,2]", "shop_concepts" => "1" ,"shop_chains.id"=> "1"}