Ruby on rails 从Rails 5中一组对象中的一个字段创建数组
给了我这样的信息:Ruby on rails 从Rails 5中一组对象中的一个字段创建数组,ruby-on-rails,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 5,给了我这样的信息: @ride.passengers 我想创建一个包含所有公司ID的数组: [ { "id": null, "salutation": "Sr", "first_name": "fsd", "last_name": "sfd", "email": "sf@sdf.sdf", "mobile": "123234345", "company_id": 7, "cost_center_id": null, "
@ride.passengers
我想创建一个包含所有公司ID的数组:
[
{
"id": null,
"salutation": "Sr",
"first_name": "fsd",
"last_name": "sfd",
"email": "sf@sdf.sdf",
"mobile": "123234345",
"company_id": 7,
"cost_center_id": null,
"created_at": null,
"updated_at": null
},
{
"id": null,
"salutation": null,
"first_name": null,
"last_name": null,
"email": null,
"mobile": null,
"company_id": 12,
"cost_center_id": null,
"created_at": null,
"updated_at": null
}
]
我试过:
[7,12]
但这将返回一个空错误。
pull
是一种活动记录方法。如果您试图操作从活动记录查询返回的数据,请重试
@ride.passengers.pluck(:company_id)
在这个数组中看起来像json。它需要是一个散列。
@rides.passengers.map{ |object| object["company_id"} }