2017-12-15 4 views
0

레일 방법 :레일 Rabl 보석 : 쿼리 아이 부모 ID를 기반으로

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

국가 모델 has_many : 도시

도시 모델 belongs_to : 국가

사용자가 특정 국가와 특정에 액세스 할 수 있습니다 도시

해당 json.rabl 파일 :

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

내 질문에 대한 child @cities.where(country_name: country.name)

내 목표는 현재 상위 국가의 도시를 포함에 @cities을 필터링하는 것입니다.

rabl에서 가능합니까?

답변

0

@cities에 인스턴스 변수를 사용하는 대신 Country 모델에 연관을 사용할 수 있습니다.

내가 가진 한 가지 질문은 사용자를위한 인덱스 방법입니까?

이런 식으로 뭔가 작동 할 수 있습니다

object @user 
child :countries do 
    child :cities do 
    attributes :name, :coordinates 
    end 
end