简体   繁体   中英

Need to Get N number of records using random function in rails

I am working one rails project,

i had a requirement like,

I have a User model which is having 1000 records. i need to get 3 records randomly out of thousand at a time.

if i use rand() function it will be applicable to getting to only one record at a time.

can anybody tell me how to write the rand() function for getting N number of records

Use it. i think this is helpful for you

@users = User.order('RAND()').limit(3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM