Rails方法之——pluck
2018-09-27 本文已影响0人
小小修行者
create_table "users", force: :cascade do |t|
t.string "name"
t.integer "age"
t.string "phone"
t.string "status"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
id: 1,
name: "aaaaaa",
age: 13,
phone: "130999999999",
status: "use",
id: 2,
name: "bb",
age: 18,
phone: "123123123",
status: "use",
以上图为例,在控制台中使用
[26] pry(main)> User.pluck(:name, :phone)
则会返回
(0.2ms) SELECT "users"."name", "users"."phone" FROM "users"
=> [["aaaaaa", "130999999999"], ["bb", "123123123"]]
可以看出pluck方法重users表中选了每一条数据的两个字段name和phone作为数组。