0

これはこの質問のフォローアップです:JSONはRailsビューにネストされていません

モデルの階層があり、それぞれが1:多くのモデルが下降するアプリケーションがあります。下から2番目のレベルには、LessonLayoutというモデルがあり、2つの子があり、0:manyの関係が可能です。子はLayoutFieldおよびLayoutTableと呼ばれます。

これまでのところ、私はこのコードを以下にリストしています(SOユーザーmeagarの助けを借りて)。

def show
  @course = Course.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.json do
      render :json => @course.to_json(:include => { :units => { :include => {:lessons => {:include => { :lesson_layouts => { :include => :layout_fields, :include => :layout_tables}}}}}})
    end
  end
end

:layout_fieldsまたは:layout_tablesオブジェクトのいずれかを持つレッスンレイアウト。現在、:layout_tablesデータを生成しますが、: layout_fieldsは表示しません。ルビーコードのオブジェクトを逆にすると、逆になります。両方がJSONで返されることを望んでいます。

コードをいじってみましたが、どこにも行きません。前もって感謝します。

4

1 に答える 1

0

私はそれを解決しました。次のように、2つのピアオブジェクトを配列に渡します。

def show
  @course = Course.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.json do
      render :json => @course.to_json(:include => { :units => { :include => {:lessons => {:include => { :lesson_layouts => { :include => [:layout_fields, :layout_tables]}}}}})
    end
  end
end
于 2012-10-26T16:37:55.697 に答える