Sha256: 367f4a9ec7ceb4d1efbb14a5d9e1b6491a12d198e32af7e1324b0a0f9e56e043

Contents?: true

Size: 793 Bytes

Versions: 4

Compression:

Stored size: 793 Bytes

Contents

#:nodoc:
module Sequel
  #:nodoc:
  class Model
    ##
    # Retrieves all primary values and an optional column and returns the results as 
    # an array of hashes in which the keys of these hashes are the IDs and the values 
    # the values of the specified column. This array can be used when creating <select> 
    # elements using the BlueForm helper.
    #
    # @example
    #  Sections::Model::Section.pk_hash(:name) # => {1 => 'Blog', 2 => 'General'}
    #
    # @author Yorick Peterse
    # @param  [Symbol] column The name of the optional column to select.
    # @return [Hash]
    #
    def self.pk_hash column
      hash = {}
      
      self.select(:id, column.to_sym).each do |row|
        hash[row.id] = row.send(column)
      end
      
      return hash
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
zen-0.2.7 lib/zen/model/methods.rb
zen-0.2.6.1 lib/zen/model/methods.rb
zen-0.2.6 lib/zen/model/methods.rb
zen-0.2.5 lib/zen/model/methods.rb