-
-
Save BMorearty/8459403 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ActiveRecord.method_defined? :pluck | |
raise "Upgraded to Rails 3.2, eh? It's time to remove the activerecord31_pluck.rb monkeypatch." | |
end | |
module ActiveRecord | |
class Base | |
class << self | |
delegate :pluck, :to =>:scoped | |
end | |
end | |
class CollectionProxy | |
delegate :pluck, :to => :scoped | |
end | |
# = Active Record Relation | |
class Relation | |
# Returns <tt>Array</tt> with values of the specified column name | |
# The values has same data type as column. | |
# | |
# Examples: | |
# | |
# Person.pluck(:id) # SELECT people.id FROM people | |
# Person.uniq.pluck(:role) # SELECT DISTINCT role FROM people | |
# Person.where(:confirmed => true).limit(5).pluck(:id) | |
# | |
def pluck(column_name) | |
if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s) | |
column_name = "#{quoted_table_name}.#{column_name}" | |
end | |
scope = self.select(column_name) | |
self.connection.select_values(scope.to_sql).map! do |value| | |
type_cast_using_column(value, column_for(column_name)) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment