我已經得到了通過第三個連接兩種型號:語法錯誤:包括
Playlist has_many :song_in_playlists, :dependent => :destroy
has_many :songs, :through => :song_in_playlists
Song has_many :song_in_playlists, :dependent => :destroy
has_many :playlists, :through => :song_in_playlists
的song_in_playlist
表只列出了playlist_id
,song_id
,以及一個track_number
。
當調用/playlists/1.json
,我想獲得一個JSON哈希保存播放列表的屬性和具有內另一哈希值,其中包含的歌曲(如/songs.json
加上track_number
從song_in_playlists
表項)。
據the Rails API,我應該能夠做到這一點執行以下操作:
def show
@playlist = Playlist.find(params[:id])
[...]
respond_to do |format|
format.html # show.html.erb
format.json { render json: @playlist.as_json(
:include => { :song_in_playlists => {
:only => :track_number, :include => :songs {
:except => [:dropbox_link, :local_path] } } }
) }
end
end
end
不過,我做什麼,得到的是如下:
/home/sloth/audiomixer/app/controllers/playlists_controller.rb:30: syntax error, unexpected '{', expecting '}'
:only => :track_number, :include => :songs {
^
/home/sloth/audiomixer/app/controllers/playlists_controller.rb:31: syntax error, unexpected '}', expecting keyword_end
:except => [:dropbox_link, :local_path] } } }
^
/home/sloth/audiomixer/app/controllers/playlists_controller.rb:103: syntax error, unexpected $end, expecting keyword_end
於是,我就有點不太複雜,打電話
format.json { render json: @playlist.as_json(
:include => song_in_playlists {
:only => :track_number }) }
但我收到類似的錯誤。只要我在括號中加上括號,Rails就會抱怨,儘管API文檔說明了相反的情況。
我真的不知道該怎麼再嘗試了:(
PS:紅寶石1.9.3,導軌3.2.13
---編輯--- 好吧,我不噸真的知道爲什麼,但我得到這個產生預期的輸出:試圖在:song
和, :only
之間,我又收到同樣的錯誤添加{ :except => [:dropbox_link, :local_path] }
當
format.json { render json: @playlist.as_json(
:include => { :song_in_playlists => {
:include => :song, :only => :track_number } }) }
然而,..
哦,我的,非常感謝。我真的不知道爲什麼我一直在':song'附近忘記大括號,儘管之前':song_in_playlists'只有幾個字。經典:p – kah