【发布时间】:2018-11-06 09:59:33
【问题描述】:
我想加入 2 个表中的项目。有输出:
"costs":[
{
"id":2,
"cost_name":"rent office",
"user_id":2,
"fix":true,
"amount":300300,
"created_at":"2018-11-05T18:36:19.108+06:00",
"updated_at":"2018-11-05T18:36:19.108+06:00"
},
{
"id":3,
"cost_name":"new computer",
"user_id":2,
"fix":false,
"amount":350000,
"created_at":"2018-11-06T14:44:49.805+06:00",
"updated_at":"2018-11-06T14:44:49.805+06:00"
}
],
"users":[
[
"Vi_Ok",
2
]
]
}
我想将用户参数(用户名“Vi_Ok”)添加到每个成本中。您如何注意到两个表中都存在 userId。现在代码看起来:
def index
@costs = Cost.all
@user_name = @costs.pluck(:user_id)
@user_name = User.find(@user_name).pluck(:name, :id)
# @costs.push("name" => @user_name.pluck(:name) this one just try to add
render json: {costs: @costs, name: @user_name}
结束
【问题讨论】:
-
用户对所有成本都是唯一的还是对每个成本都不同?
-
@UdAY 是的,看起来是这样。
-
成本和用户之间的关联是什么?
-
@ray 提供的数据看起来如此。如果用户随成本变化怎么办?
标签: ruby-on-rails json ruby api rendering