From a382d60f6abc94b6a965525872f858e48abc00de Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Wed, 30 Nov 2011 11:03:00 +0200 Subject: ActiveRecord::Relation#pluck method --- railties/guides/source/active_record_querying.textile | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'railties/guides') diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index c4724f182e..073f7c143d 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -1146,6 +1146,15 @@ h3. +select_all+ Client.connection.select_all("SELECT * FROM clients WHERE id = '1'") +h3. +pluck+ + +pluck 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. + + +Client.where(:active => true).pluck(:id) # SELECT id FROM clients WHERE clients.active +Client.uniq.pluck(:role) # SELECT DISTINCT role FROM clients + + h3. Existence of Objects If you simply want to check for the existence of the object there's a method called +exists?+. This method will query the database using the same query as +find+, but instead of returning an object or collection of objects it will return either +true+ or +false+. -- cgit v1.2.3