Find random objects using conditions

2

I want to search for example 10 random people of the same party I did the following, but this is selecting people from all parties, what can be done? = D

class Pessoa < ActiveRecord::Base

    class << self
        def filtra_pessoas()
            Pessoa.where('rand()', partido: "sem partido").first(10)
        end
    end
end
    
asked by anonymous 28.09.2016 / 07:17

1 answer

3

Hello, just pass rand() to order . Example:

class Pessoa < ActiveRecord::Base

    class << self
        def filtra_pessoas()
            Pessoa.where(partido: "sem partido").order('rand()').first(10)
        end
    end
end

GG

    
28.09.2016 / 14:01