Conditionally include child nodes with RABL

I think I have a very simple case where I use rabl to return JSON for standard RESTful controller methods ...

First, let's say that I have a typical relationship with many relationships with the parent and child (say, the client and the accounts):

class Customer < ActiveRecord::Base has_many :accounts end 

In my controller for the client, I have an index and editing - an index where I just want to return all clients (WITHOUT accounts) and change where I want to return a specific Client with all my accounts. With an index, I don't want to return accounts for obvious performance (N + 1).

In my rabl, I have the following:

 #index.rabl (for listing all customers) collection :@customers extends "customers/_customer" 

and

 #edit.rabl (for editing a single customer) object :@customer extends "customer/_customer" 

I reuse _customer.rabl for both index and editing.

 #_customer.rabl object :@customer attributes :name, ...... if @include_accounts child :accounts do extends "accounts/_account" end end 

I set @include_accounts in my editor (controller), and not in the index, but this will not work - essentially the var instance here (@include_accounts) is never passed. What is the correct template to implement this?

+6
source share
1 answer

Although the rails-rabl stone claims to be faster, I found that templates are not so flexible for conditional expressions - see rabl-rails readme re: instance variables.

+3
source

All Articles