Rails select random record

前端 未结 8 1759
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 03:46

I don\'t know if I\'m just looking in the wrong places here or what, but does active record have a method for retrieving a random object?

Something like?

<         


        
相关标签:
8条回答
  • 2020-12-03 03:55

    Strongly Recommend this gem for random records, which is specially designed for table with lots of data rows:

    https://github.com/haopingfan/quick_random_records

    Simple Usage:

    @user = User.random_records(1).take


    All other answers perform badly with large database, except this gem:

    1. quick_random_records only cost 4.6ms totally.

    1. the accepted answer User.order('RAND()').limit(10) cost 733.0ms.

    1. the offset approach cost 245.4ms totally.

    1. the User.all.sample(10) approach cost 573.4ms.

    Note: My table only has 120,000 users. The more records you have, the more enormous the difference of performance will be.


    UPDATE:

    Perform on table with 550,000 rows

    1. Model.where(id: Model.pluck(:id).sample(10)) cost 1384.0ms

    1. gem: quick_random_records only cost 6.4ms totally

    0 讨论(0)
  • 2020-12-03 03:56

    I'd use a named scope. Just throw this into your User model.

    named_scope :random, :order=>'RAND()', :limit=>1
    

    The random function isn't the same in each database though. SQLite and others use RANDOM() but you'll need to use RAND() for MySQL.

    If you'd like to be able to grab more than one random row you can try this.

    named_scope :random, lambda { |*args| { :order=>'RAND()', :limit=>args[0] || 1 } }
    

    If you call User.random it will default to 1 but you can also call User.random(3) if you want more than one.

    0 讨论(0)
  • 2020-12-03 03:58

    Try using Array's sample method:

    @user = User.all.sample(1)
    
    0 讨论(0)
  • 2020-12-03 04:00

    Most of the examples I've seen that do this end up counting the rows in the table, then generating a random number to choose one. This is because alternatives such as RAND() are inefficient in that they actually get every row and assign them a random number, or so I've read (and are database specific I think).

    You can add a method like the one I found here.

    module ActiveRecord
      class Base
        def self.random
          if (c = count) != 0
            find(:first, :offset =>rand(c))
          end
        end
      end
    end
    

    This will make it so any Model you use has a method called random which works in the way I described above: generates a random number within the count of the rows in the table, then fetches the row associated with that random number. So basically, you're only doing one fetch which is what you probably prefer :)

    You can also take a look at this rails plugin.

    0 讨论(0)
  • 2020-12-03 04:01

    In Rails 4 I would extend ActiveRecord::Relation:

    class ActiveRecord::Relation
      def random
        offset(rand(count))
      end
    end
    

    This way you can use scopes:

    SomeModel.all.random.first # Return one random record
    SomeModel.some_scope.another_scope.random.first
    
    0 讨论(0)
  • 2020-12-03 04:03

    Here is the best solution for getting random records from database. RoR provide everything in ease of use.

    For getting random records from DB use sample, below is the description for that with example.

    Backport of Array#sample based on Marc-Andre Lafortune’s github.com/marcandre/backports/ Returns a random element or n random elements from the array. If the array is empty and n is nil, returns nil. If n is passed and its value is less than 0, it raises an ArgumentError exception. If the value of n is equal or greater than 0 it returns [].

    [1,2,3,4,5,6].sample     # => 4     
    [1,2,3,4,5,6].sample(3)  # => [2, 4, 5]     
    [1,2,3,4,5,6].sample(-3) # => ArgumentError: negative array size     
    [].sample     # => nil     
    [].sample(3)  # => []     
    

    You can use condition with as per your requirement like below example.

    User.where(active: true).sample(5)

    it will return randomly 5 active user's from User table

    For more help please visit : http://apidock.com/rails/Array/sample

    0 讨论(0)
提交回复
热议问题