I've got two models that are connected through a third one:
我有两个通过第三个连接的模型:
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
The song_in_playlist
table simply lists the playlist_id
, song_id
, as well as a track_number
.
song_in_playlist表简单地列出了playlist_id,song_id以及track_number。
When calling /playlists/1.json
, I'd like to get a json hash that holds the playlist's attributes and has another hash inside, which contains the songs (as in /songs.json
plus the track_number
from the song_in_playlists
table entry).
在调用/playlists/1.json时,我想获得一个包含播放列表属性的json哈希,并且内部有另一个哈希,其中包含歌曲(如/songs.json加上song_in_playlists表条目中的track_number)。
According to the Rails API, I should be able to achieve this doing the following:
根据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
However, what I do get is the following:
但是,我得到的是以下内容:
/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
So I tried a little less complexity, calling
所以我尝试了一点点复杂性,打电话
format.json { render json: @playlist.as_json(
:include => song_in_playlists {
:only => :track_number } ) }
but I'm getting a similar error. As soon as I'm putting any braces in between the parentheses, Rails complains, although the API documentation states the opposite.
但我得到了类似的错误。一旦我在括号之间添加任何括号,Rails就会抱怨,尽管API文档说明了相反的情况。
I really don't know what to try anymore :(
我真的不知道该怎么办了:(
PS: Ruby 1.9.3, Rails 3.2.13
PS:Ruby 1.9.3,Rails 3.2.13
--- EDIT --- Okay, I don't really know why but I got this to produce the expected output:
---编辑---好的,我真的不知道为什么,但我得到这个产生预期的输出:
format.json { render json: @playlist.as_json(
:include => { :song_in_playlists => {
:include => :song, :only => :track_number } } ) }
However, when trying to add { :except => [:dropbox_link, :local_path] }
in between :song
and , :only
, I'm getting the same errors again..
但是,在尝试添加{:except => [:dropbox_link,:local_path]}之间:song和,:only,我再次遇到相同的错误..
1 个解决方案
#1
3
This is only partly about rails
这只是部分关于铁路
:include => song_in_playlists {:only => :track_number}
just isn't valid ruby syntax
只是不是有效的ruby语法
this should be
这应该是
:include => {:song_in_playlists => {:only => :track_number}}
Each time, the key is the association name (song_in_playlists) and the value is hash containing one or more of the keys :only
, :except
or :include
每次,键是关联名称(song_in_playlists),值是包含一个或多个键的哈希:only,:except或:include
If you want to change what attributes from the songs table are in the json then you need to take
如果你想更改json中歌曲表中的属性,那么你需要采取
format.json { render json: @playlist.as_json(
:include => { :song_in_playlists => {
:include => :song, :only => :track_number } } ) }
and replace :song
with a hash that describes what you want to do with song, for example
并替换:歌曲,其中包含描述您要对歌曲做什么的哈希值
format.json { render json: @playlist.as_json(
:include => {
:song_in_playlists => {
:include => {
:song => {
:except => [:dropbox_link, :local_path]
}
},
:only => :track_number
}
})}
#1
3
This is only partly about rails
这只是部分关于铁路
:include => song_in_playlists {:only => :track_number}
just isn't valid ruby syntax
只是不是有效的ruby语法
this should be
这应该是
:include => {:song_in_playlists => {:only => :track_number}}
Each time, the key is the association name (song_in_playlists) and the value is hash containing one or more of the keys :only
, :except
or :include
每次,键是关联名称(song_in_playlists),值是包含一个或多个键的哈希:only,:except或:include
If you want to change what attributes from the songs table are in the json then you need to take
如果你想更改json中歌曲表中的属性,那么你需要采取
format.json { render json: @playlist.as_json(
:include => { :song_in_playlists => {
:include => :song, :only => :track_number } } ) }
and replace :song
with a hash that describes what you want to do with song, for example
并替换:歌曲,其中包含描述您要对歌曲做什么的哈希值
format.json { render json: @playlist.as_json(
:include => {
:song_in_playlists => {
:include => {
:song => {
:except => [:dropbox_link, :local_path]
}
},
:only => :track_number
}
})}