vendredi 15 décembre 2017

Rails Rabl gem: query child based on parent id

The rails method:

def index
 user = Users.find(params[:user_id])
 @countries = user.countries
 @cities = user.cities
end

Country model has_many :cities

City model belongs_to :country

User has access to certain countries and certain cities

The corresponding json.rabl file:

object false
child @countries do 
 child @cities.where(country_name: country.name) do
  attributes :name, :coordinates
 end
end

My question is about child @cities.where(country_name: country.name)

My goal is to filter the @cities to only include the cities for the current parent country.

Is this possible in rabl?

Aucun commentaire:

Enregistrer un commentaire