Skip to content

Commit

Permalink
Activerecord 4.1 is really not work supporting :sigh:
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Zane committed Feb 9, 2017
1 parent 9fe8fc5 commit fe04c08
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/baby_squeel/active_record/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,38 @@ def when_having(&block)
having DSL.evaluate(self, &block)
end


if ::ActiveRecord::VERSION::MAJOR >= 5
def plucking(&block)
pluck DSL.evalulate(self, &block)
pluck DSL.evaluate(self, &block)
end
else
elsif ::ActiveRecord::VERSION::STRING >= '4.2.0'
def plucking(&block)
relation = selecting(&block)
binds = relation.arel.bind_values + bind_values
result = klass.connection.select_all(relation.arel, nil, binds)
result.cast_values(klass.column_types)
end
else
def plucking(&block)
relation = selecting(&block)
binds = relation.arel.bind_values + bind_values
result = klass.connection.select_all(relation.arel, nil, binds)
columns = result.columns.map do |key|
klass.column_types.fetch(key) {
result.column_types.fetch(key) { result.identity_type }
}
end

result = result.rows.map do |values|
values = result.columns.zip(values).map do |column_name, value|
single_attr_hash = { column_name => value }
klass.initialize_attributes(single_attr_hash).values.first
end

columns.zip(values).map { |column, value| column.type_cast value }
end
columns.one? ? result.map!(&:first) : result
end
end

private
Expand Down

0 comments on commit fe04c08

Please sign in to comment.