Search for random objects using conditions

Asked

Viewed 54 times

2

I want to seek for example 10 random people of the same party I did as follows, but this 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

1 answer

3


Hello, just pass the rand() as order. Example:

class Pessoa < ActiveRecord::Base

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

GG

  • It worked, thanks. when I use this method again it does not list, only replaces the search can you tell how I can solve it? = D example: filtra_people() // search "10" persons "without party" filtra_people() // search "8" person of "such party"

  • @Erasmosantos creates another question and explains how I help you :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.