2012-12-10 126 views
0

我有兩個型號正文部分和exercises.Both都具有has_and_belongs_to_many關係嵌套路線3

Bodyparts 
has_and_belongs_to_many :exercises 

Exercises 
has_and_belongs_to_many :bodyparts 

在我的routes.rb文件我已經給了,

resources :bodyparts do 
    resources :exercises 
end 

我想所有的取練習屬於特定的身體部位。

我已經測試http://localhost:3000/bodyparts/1/exercises.json

,但我得到的鍛鍊陣列。

如何解決這個問題。

在此先感謝.....

回答

0

你需要指定你的控制器

def index 
    @exercises = BodypartExercise.where(:muscle_id => params['muscle_id']) 

    respond_to do |format| 
    format.html # index.html.erb 
    format.json { render json: @exercises } 
    end 
end 
+0

兩款車型都具有「has_and_belongs_to_many」 relations.I還創建了一個映射表「bodyparts_exercises」來地圖bodyparts_id和Exercises_id。您提供的答案最適合has_many關係。它不適用於HABTM關係。 – Cyber

+0

然後你需要BodypartsExercise.where(:muscle_id => params ['muscle_id']) – Baconator507

+0

bodyparts_exercises只是一個通過遷移創建的表,它不是一個模型。 – Cyber