diff options
author | Vijay Dev <vijaydev.cse@gmail.com> | 2011-12-01 01:23:19 +0530 |
---|---|---|
committer | Vijay Dev <vijaydev.cse@gmail.com> | 2011-12-01 01:42:54 +0530 |
commit | cbeeaa6ea000d7a6e6ae90de5b6dc756d15240ab (patch) | |
tree | e7bb9845296503ed164f5302b376963b621d5cf2 /railties/guides | |
parent | a72839bc9dc5fb3d56e79f7dc7f85975550d6fde (diff) | |
download | rails-cbeeaa6ea000d7a6e6ae90de5b6dc756d15240ab.tar.gz rails-cbeeaa6ea000d7a6e6ae90de5b6dc756d15240ab.tar.bz2 rails-cbeeaa6ea000d7a6e6ae90de5b6dc756d15240ab.zip |
expand on pluck docs
Diffstat (limited to 'railties/guides')
-rw-r--r-- | railties/guides/source/active_record_querying.textile | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 073f7c143d..352f23dc01 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -1148,11 +1148,26 @@ Client.connection.select_all("SELECT * FROM clients WHERE id = '1'") h3. +pluck+ -<tt>pluck</tt> can be used to query single column from table under model. It accepts column name as argument and returns Array of values of the specified column with corresponding data type. +<tt>pluck</tt> can be used to query a single column from the underlying table of a model. It accepts a column name as argument and returns an array of values of the specified column with the corresponding data type. <ruby> -Client.where(:active => true).pluck(:id) # SELECT id FROM clients WHERE clients.active -Client.uniq.pluck(:role) # SELECT DISTINCT role FROM clients +Client.where(:active => true).pluck(:id) +# SELECT id FROM clients WHERE active = 1 + +Client.uniq.pluck(:role) +# SELECT DISTINCT role FROM clients +</ruby> + ++pluck+ makes it possible to replace code like + +<ruby> +Client.select(:id).map { |c| c.id } +</ruby> + +with + +<ruby> +Client.pluck(:id) </ruby> h3. Existence of Objects |