【发布时间】:2019-02-05 00:49:39
【问题描述】:
我使用.select 来选择要返回的字段,我指定了除:id, :created_at and :updated_at 之外的所有字段,无论如何它仍然返回"id": null。
我正在使用 Ruby on Rails 5 构建一个 API,像往常一样,字段 :id、:created_at 和 :updated_at 是在运行迁移后创建的,其中 :id 是主键。
当我使用address.select(:name, :region, :province)时
我明白了:
[
{
"id": null,
"name": "Lorem Ipsum",
"region": "some text",
"province": "some more text"
},
{
"id": null,
"name": "Lorem Ipsum 1",
"region": "some text 1",
"province": "some more text 1"
}
]
但我期待的是:
[
{
"name": "Lorem Ipsum",
"region": "some text",
"province": "some more text"
},
{
"name": "Lorem Ipsum 1",
"region": "some text 1",
"province": "some more text 1"
}
]
希望我的解释已经足够了。
【问题讨论】:
标签: ruby-on-rails ruby select activerecord ruby-on-rails-5