开发者

users collection1 minus users collection2

开发者 https://www.devze.com 2023-03-19 10:00 出处:网络
I am using Rails 3.0.8 Lets say that I have two collections of users. users1 = User.where(....) users2 = User.where(....)

I am using Rails 3.0.8

Lets say that I have two collections of users.

users1 = User.where(....)
users2 = User.where(....)

I need

users1 - users2

One way to get the solution is

ids开发者_如何学编程 = users1.map(&:id) - users2.map(&:id)
ids.map{ |i| User.find(i) }

Above code will work. However I was wondering if ActiveRecord implements some kind of equality operator so that I do not have to get ids from that collection.


users1 - users2 works perfectly, keep it simple!

0

精彩评论

暂无评论...
验证码 换一张
取 消