Kris MP
Kris MP

Reputation: 2415

Rails activerecord get user with id dividable by 3

How to get user who has an ID dividable by 3

something like :

User.where(id: id % 3 == 0)

thanks

Upvotes: 2

Views: 66

Answers (2)

arogachev
arogachev

Reputation: 33548

You can use pure string condition here, because data is static (not coming from user for example):

User.where('id % 3 = 0')

Read more in official docs.

Upvotes: 2

Atri
Atri

Reputation: 5831

If the database is Oracle, then you can do the following:

User.where('mod(id, 3) = 0')

Query:

2.1.2-perf :004 > User.where('mod(id, 3) = 0').to_sql
 => "SELECT \"users\".* FROM \"users\"  WHERE (mod(id, 3) = 0)"

Sql fiddle demo for this query here.

Upvotes: 2

Related Questions