我有一个模型客户,有很多:旅游,协会在其中。我用这个代码来为所有的客户提供他们的服务,
render :json => Client.all(:include => :tours)现在,需求发生了变化,因此该对象表示只能加载用户的浏览。每一次巡回演出都与一个有关系的用户相关联,用户有很多:旅游。我试过了
render :json => Client.all(:include => :tours, :conditions => ["tours.user_id = ?", params[:user_id]])这让我的客户有该用户的旅游,但列出了这些客户的所有旅游。但是,我只希望该用户的旅游只列在每个客户端下面。我可以用:includes来做吗?
client.rb
has_many :tours, :dependent => :destroyuser.rb
has_may :tours, :dependent => :destroy更新
我认为最好增加一个例子来解释我的问题。假设有三个客户: A,B,C。
现在,如果我们使用我的方法获得user1的响应,它将如下所示:
[
{
"name": "Client A",
....
"tours": [
{
"name": "Tour1",
....
},
{
"name": "Tour2",
......
},
{
"name": "Tour3",
.....
}
]
},
{
"name": "Client C",
....
"tours": [
{
"name": "Tour4",
...
},
{
"name": "Tour5",
...
},
{
"name": "Tour6",
...
}
]
}
] 您可以看到客户端B被省略,客户端A和客户端C被包括在内,这是正确的。但是,对于客户机C,只有Tour5属于用户user1。但是,它的所有旅游都包括在内。我想要我对省略Tour5和Tour6的回应如下:
[
{
"name": "Client A",
....
"tours": [
{
"name": "Tour1",
....
},
{
"name": "Tour2",
......
},
{
"name": "Tour3",
.....
}
]
},
{
"name": "Client C",
....
"tours": [
{
"name": "Tour4",
...
}
]
}
] 发布于 2011-11-10 21:30:35
我只是猜,但你有没有尝试过这样的方式:
render :json => Client.all(:include => {:tours => :user}, :conditions => ["users.id = ?", params[:user_id]])https://stackoverflow.com/questions/8082270
复制相似问题