我很困惑如何在我的网络中找到未使用rails方法set
邀请参加活动/社交活动的朋友列表。
friends
列出了我所有的朋友(总共2个朋友)social_invited_friends
social_univited_friends
和friends
" social_invited_friends
有人可以建议我怎么做吗?
这是终端
2.3.0 :207 > ap friends = Set.new((inviter.friends).to_a)
[
[0] #<User:0x007fa6f882adb0> {
:id => 2,
:email => "emma@gmail.com"
},
[1] #<User:0x007fa6f7191db0> {
:id => 4,
:email => "jesse@gmail.com"
}
]
2.3.0 :213 > ap social_invited_friends = Set.new(Invite.where(user: inviter, invitee: nil, social: social).to_a.map(&:users))
Invite Load (0.2ms) SELECT "invites".* FROM "invites" WHERE "invites"."user_id" = 1 AND "invites"."invitee_id" IS NULL AND "invites"."social_id" = 6
User Load (0.1ms) SELECT "users".* FROM "users" INNER JOIN "invites_users" ON "users"."id" = "invites_users"."user_id" WHERE "invites_users"."invite_id" = ? [["invite_id", 30]]
User Load (0.3ms) SELECT "users".* FROM "users" INNER JOIN "invites_users" ON "users"."id" = "invites_users"."user_id" WHERE "invites_users"."invite_id" = ? [["invite_id", 31]]
[
[0] [
[0] #<User:0x007fa6f7191f90> {
:id => 2,
:email => "emma@gmail.com"
},
[1] #<User:0x007fa6f7191db0> {
:id => 4,
:email => "jesse@gmail.com"
}
]
]
=> nil
2.3.0 :214 >
当我尝试以下
时social_univited_friends = friends.difference(social_invited_friends)
我在终端
中得到以下结果2.3.0 :207 > ap social_univited_friends = friends.difference(social_invited_friends)
[
[0] #<User:0x007fa6f882adb0> {
:id => 2,
:email => "emma@gmail.com"
},
[1] #<User:0x007fa6f7191db0> {
:id => 4,
:email => "jesse@gmail.com"
}
]
但我相信我应该没有任何回报,因为我邀请了所有朋友参加活动/社交活动 - 我应该有类似下面的内容:
2.3.0 :207 > ap social_univited_friends = friends.difference(social_invited_friends)
[ ]
答案 0 :(得分:1)
仔细查看ap social_invited_friends
:
[
[0] [
[0] #<User ...
[1] #<User ...
]
]
然后在ap friends
:
[
[0] #<User ...
[1] #<User ...
]
social_invited_friends
是一个包含一个元素的集合,该元素是User
个实例的两个元素数组; friends
是一个包含两个User
个实例的元素的集合。
在构建map
时,您可能希望使用flat_map
而不是social_invited_friends
:
Set.new(
Invite.where(...)
.flat_map(&:users)
)
flat_map
会处理所有内容的数组,因此您在构建to_a
时也可以放弃social_invited_friends
调用。
你也应该能够在数据库中完成整个工作,但要先让它工作。