2.2.3 :006 > a.each do |x|
2.2.3 :007 > puts x.body
2.2.3 :008?> end
comment123
+----+------------+------------------------+-------------------------+---------+
| id | body | created_at | updated_at | post_id |
+----+------------+------------------------+-------------------------+---------+
| 1 | comment123 | 2016-02-04 07:00:34... | 2016-02-04 07:47:09 UTC | 221 |
+----+------------+------------------------+-------------------------+---------+
1 row in set
2.2.3 :009 > a.all
Comment Load (0.5ms) SELECT "comments".* FROM "comments" WHERE (post_id = 221)
+----+------------+------------------------+-------------------------+---------+
| id | body | created_at | updated_at | post_id |
+----+------------+------------------------+-------------------------+---------+
| 1 | comment123 | 2016-02-04 07:00:34... | 2016-02-04 07:47:09 UTC | 221 |
+----+------------+------------------------+-------------------------+---------+
1 row in set
2.2.3 :010 > a
+----+------------+------------------------+-------------------------+---------+
| id | body | created_at | updated_at | post_id |
+----+------------+------------------------+-------------------------+---------+
| 1 | comment123 | 2016-02-04 07:00:34... | 2016-02-04 07:47:09 UTC | 221 |
+----+------------+------------------------+-------------------------+---------+
1 row in set
2.2.3 :011 > a = Comment.where("post_id = 221").pluck("body")
(0.5ms) SELECT "comments"."body" FROM "comments" WHERE (post_id = 221)
[
[0] "comment123"
]
2.2.3 :012 > a = Comment.where(post_id: 221).pluck(:body, :updated_at)
(0.2ms) SELECT "comments"."body", "comments"."updated_at" FROM "comments" WHERE "comments"."post_id" = $1 [["post_id", 221]]
[
[0] [
[0] "comment123",
[1] Thu, 04 Feb 2016 07:47:09 UTC +00:00
]
]
2.2.3 :013 > column_names = [:body, :updated_at]
[
[0] :body,
[1] :updated_at
]
2.2.3 :014 > a = Comment.where(post_id: 221).pluck(*column_names)
(0.4ms) SELECT "comments"."body", "comments"."updated_at" FROM "comments" WHERE "comments"."post_id" = $1 [["post_id", 221]]
[
[0] [
[0] "comment123",
[1] Thu, 04 Feb 2016 07:47:09 UTC +00:00
]
]
2.2.3 :015 > Hash[column_names.zip(a[0])]
{
:body => "comment123",
:updated_at => Thu, 04 Feb 2016 07:47:09 UTC +00:00
}
2.2.3 :016 > a = Comment.where(post_id: 221).pluck(*column_names)
(0.5ms) SELECT "comments"."body", "comments"."updated_at" FROM "comments" WHERE "comments"."post_id" = $1 [["post_id", 221]]
[
[0] [
[0] "comment123",
[1] Thu, 04 Feb 2016 07:47:09 UTC +00:00
]
]
2.2.3 :017 > a.map{|x| Hash[column_names.zip(x)]}
[
[0] {
:body => "comment123",
:updated_at => Thu, 04 Feb 2016 07:47:09 UTC +00:00
}
]
Created
February 5, 2016 01:18
-
-
Save jennli/52b7b0e498c52074db26 to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment