Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从Rails 5中一组对象中的一个字段创建数组_Ruby On Rails_Ruby On Rails 5 - Fatal编程技术网

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"} }