2

例如,我想运行如下查询

people = Person.select("GROUP_CONCAT(`first` SEPARATOR ', ') as names") \
.where(last: "smith").group(:age).order(:age)

# which basically gives me something like
# SELECT GROUP_CONCAT(`first` SEPARATOR ', ') as names ...

#####################

# but when I add pluck
people.pluck(:names)
# it does
# SELECT names ...
# and gives me an Unknown column error

如何使 rails/activerecord 处理选择查询的结果而不是覆盖它?

4

1 回答 1

3

这是不可能的。pluck将始终覆盖您指定的任何选择。但是你可以这样做:

people = Person.where(last: "smith").group(:age).order(:age)
people.pluck("GROUP_CONCAT(`first` SEPARATOR ', ')")

或这个:

people = Person.select("GROUP_CONCAT(`first` SEPARATOR ', ') as names") \
.where(last: "smith").group(:age).order(:age)
people.all.collect(&:name)
于 2013-11-11T23:53:13.107 回答