Ruby on Rails -如何获得所有的古怪用户

时间:2023-01-14 12:18:35

I would like to get all users where user_id is odd like (1,3,5,7,9,11...) Is there any easy option to do it?

我希望获得user_id为奇数的所有用户(1、3、5、7、9、11…)有什么简单的选择吗?

@fed = Users.all

@fed = Users.all

2 个解决方案

#1


5  

@fed = Users.where("(id % 2) > 0").all # odd
@fed = Users.where("(id % 2) = 0").all # even

#2


0  

using mod function is best way to find odd results. User.where('MOD(id,2)')

使用mod函数是查找奇数结果的最好方法。User.where(MOD(id、2))

#1


5  

@fed = Users.where("(id % 2) > 0").all # odd
@fed = Users.where("(id % 2) = 0").all # even

#2


0  

using mod function is best way to find odd results. User.where('MOD(id,2)')

使用mod函数是查找奇数结果的最好方法。User.where(MOD(id、2))