select-column 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README +64 -0
- data/Rakefile +1 -0
- data/lib/select-column/base.rb +7 -0
- data/lib/select-column/finder_methods.rb +18 -0
- data/lib/select-column/version.rb +5 -0
- data/lib/select-column.rb +2 -0
- data/select-column.gemspec +20 -0
- metadata +65 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
= Select Column
|
2
|
+
|
3
|
+
== Description
|
4
|
+
|
5
|
+
Select a single column from a table by passing ActiveRecord object creation.
|
6
|
+
|
7
|
+
== Summary
|
8
|
+
|
9
|
+
Many times when we are selecting a rows out of the database we just want a single column and have no need for the entire object. There are a number of ways to accomplish this with ActiveRecord. One can get all the records from the database and then collect the attribute needed:
|
10
|
+
|
11
|
+
Posts.where(:status => 'published').collect(&:id)
|
12
|
+
=> [ 1, 5, 8, 10 ]
|
13
|
+
|
14
|
+
This has the benefit of being able to us any overwritten accessors, but has a lot of overhead associated with generating the objects. Another way to do it is to go directly to the database:
|
15
|
+
|
16
|
+
ActiveRecord::Base.connection.select_values("SELECT id FROM posts WHERE status = 'published'")
|
17
|
+
=> [ 1, 5, 8, 10 ]
|
18
|
+
|
19
|
+
This is much faster, but requires one to use the direct connection to the database and have the SQL literal prepared. Not particularly user friendly even if you can get the SQL literal using the to_sql:
|
20
|
+
|
21
|
+
ActiveRecord::Base.connection.select_values(Posts.where(:status => 'published').select(:id).to_sql)
|
22
|
+
=> [ 1, 5, 8, 10 ]
|
23
|
+
|
24
|
+
Wouldn't it be nicer if you could just do the following:
|
25
|
+
|
26
|
+
Post.where(:status => 'published').select_column(:id)
|
27
|
+
=> [ 1, 5, 8, 10 ]
|
28
|
+
|
29
|
+
== Usage
|
30
|
+
|
31
|
+
select_column accepts a single optional argument. This is the column that you want to have returned in an array. The returned column can also be specified using the select query method.
|
32
|
+
|
33
|
+
If neither a select nor an argument is given, :id is assumed to be the column to be returned. If multiple select query methods are present, the first one defined will be the column returned.
|
34
|
+
|
35
|
+
Some examples:
|
36
|
+
|
37
|
+
Post.select_column
|
38
|
+
Post.select_column(:id)
|
39
|
+
Post.where(:status => 'published').select_column
|
40
|
+
Post.where(:status => 'published').select_column(:id)
|
41
|
+
Post.select(:id).where(:status => 'published').select_column
|
42
|
+
|
43
|
+
== License
|
44
|
+
|
45
|
+
Copyright (c) 2011 Les Fletcher
|
46
|
+
|
47
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
48
|
+
a copy of this software and associated documentation files (the
|
49
|
+
"Software"), to deal in the Software without restriction, including
|
50
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
51
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
52
|
+
permit persons to whom the Software is furnished to do so, subject to
|
53
|
+
the following conditions:
|
54
|
+
|
55
|
+
The above copyright notice and this permission notice shall be
|
56
|
+
included in all copies or substantial portions of the Software.
|
57
|
+
|
58
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
59
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
60
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
61
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
62
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
63
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
64
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module FinderMethods
|
3
|
+
|
4
|
+
def select_column(column_select=nil)
|
5
|
+
self.select_values = if !column_select.nil?
|
6
|
+
[ column_select ]
|
7
|
+
elsif !select_values.nil? && !select_values.empty?
|
8
|
+
[ select_values.first ]
|
9
|
+
else
|
10
|
+
[ :id ]
|
11
|
+
end
|
12
|
+
|
13
|
+
column = self.klass.columns_hash[select_values.first.to_s]
|
14
|
+
self.klass.connection.select_values(self.to_sql).map { |value| column.type_cast(value) }
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "select-column/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "select-column"
|
7
|
+
s.version = Select::Column::VERSION
|
8
|
+
s.authors = ["Les Fletcher"]
|
9
|
+
s.email = ["les.fletcher@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Select a single column from a table by passing ActiveRecord object creation.}
|
12
|
+
s.description = %q{Select a single column from a table by passing ActiveRecord object creation.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "select-column"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: select-column
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: "0.2"
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Les Fletcher
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-07-08 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Select a single column from a table by passing ActiveRecord object creation.
|
18
|
+
email:
|
19
|
+
- les.fletcher@gmail.com
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- .gitignore
|
28
|
+
- Gemfile
|
29
|
+
- README
|
30
|
+
- Rakefile
|
31
|
+
- lib/select-column.rb
|
32
|
+
- lib/select-column/base.rb
|
33
|
+
- lib/select-column/finder_methods.rb
|
34
|
+
- lib/select-column/version.rb
|
35
|
+
- select-column.gemspec
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: ""
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project: select-column
|
60
|
+
rubygems_version: 1.5.0
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Select a single column from a table by passing ActiveRecord object creation.
|
64
|
+
test_files: []
|
65
|
+
|