从Rails ActiveRecord查询中获取不同的元素

时间:2012-04-12 19:56:19

标签: ruby-on-rails activerecord

我在Rails中有这样的查询:

@addresses=People.select("first_name, state").where("first_name = 'Bob'")

有没有办法可以迭代地址并拉出不同的状态,这样我就知道某个名叫bob的州在哪些州生活?

我试图避免:

@states = []
@address.each.do |add|
  if !@states.contains?(add.state)
    @states.push add

这看起来很糟糕。

1 个答案:

答案 0 :(得分:5)

@states = People.where(first_name: 'Bob').uniq.pluck(:state)