0

I'm looking for a way to find every 10th record in my 'Lang' database starting with the first record. Something like this:

@words = Lang.find(records with ids of 1,11,21,31,41...)
MZaragoza
  • 10,108
  • 9
  • 71
  • 116
mtcrts70
  • 35
  • 4
  • 19
  • possible duplicate of [How do you select every n-th row from mysql](http://stackoverflow.com/questions/858746/how-do-you-select-every-n-th-row-from-mysql) – jvnill Jun 05 '15 at 01:25
  • @jvnill That question helps but its not active record its just mysql but thanks it was a good starting point for me – MZaragoza Jun 05 '15 at 01:50

1 Answers1

1

you can do something like this

total = Lang.count
ids = (1..total).step(10)
Lang.where(id: ids.to_a)

After thinking about this. I think if you are looking for a sample the best thing to do is

Lang.all.shuffle.first(30) # this returns 30 random rows from the langs table 

I try to do it as clean as I could

I hope that this helps

MZaragoza
  • 10,108
  • 9
  • 71
  • 116