2017-12-15 9 views
0

レール方式:Railsの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モデルで関連付けを使用できます。

私が持っている1つの質問は、これはユーザーのインデックス方法ですか?

このような何かが動作することがあります。

object @user 
child :countries do 
    child :cities do 
    attributes :name, :coordinates 
    end 
end 
関連する問題