使用限制堆叠Rails查询

时间:2012-08-24 06:39:14

标签: mysql ruby ruby-on-rails-3 scope

我试图从数据库中获得总共25个关联。我希望15的计数等于1,其余的计数大于1(因此大于10)。 我尝试过以下方法:

def self.get_specific_array
  a = Association.limit(25) # I would like a total of 25 associations
  a.where(["count = ?", 1]).limit(15) # I would like 15 of the associations to have a count of 1
  a.where(["count > ?", 1]) # I would like the remaining 10 associations to have a count that is greater then 1
  a
end

1 个答案:

答案 0 :(得分:2)

您可以添加查询的结果数组:

a.where("count = 1").limit(15) + a.where("count > 1").limit(10)