Sha256: 8eeaff98bbbbc7ab6bebb55918eed04d8803d977898703fe41b1f566a1ed6bf2

Contents?: true

Size: 767 Bytes

Versions: 6

Compression:

Stored size: 767 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'}
    #
    # @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 # Model
end # Sequel

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
zen-0.4.2 lib/zen/model/methods.rb
zen-0.4.1 lib/zen/model/methods.rb
zen-0.4 lib/zen/model/methods.rb
zen-0.3 lib/zen/model/methods.rb
zen-0.3b1 lib/zen/model/methods.rb
zen-0.3b lib/zen/model/methods.rb