所以说我有一组看起来像这样的用户:
[19] pry(main)> j.connected_users
(1.2ms) SELECT "memberships"."invited_id" FROM "memberships" WHERE (memberships.user_id = 117 OR memberships.invited_id = 117) AND ("memberships"."invited_id" IS NOT NULL) AND ("memberships"."invited_id" != 117)
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (1, 3)
=> [#<User id: 3, email: "jackie@test.com", encrypted_password: "$2a$10$kZWVB9Vu.08Dyesc74w20eod4Yh4sBnQ1XE1C2P9fXH...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 10, current_sign_in_at: "2016-02-21 08:21:13", last_sign_in_at: "2016-01-12 19:08:08", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2015-07-25 02:20:19", updated_at: "2016-02-21 08:21:13", first_name: "Jackie", confirmation_token: nil, confirmed_at: "2015-07-25 02:20:20", confirmation_sent_at: "2015-07-25 02:20:19", unconfirmed_email: nil, invitation_relation: nil, avatar: nil, invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_id: nil, invited_by_type: nil, invitations_count: 0, bio: nil, last_name: "Gayle", gender: 1, birthday: nil, storage_used_in_bytes: 0>,
#<User id: 1, email: "abc@test.com", encrypted_password: "$2a$10$IQXTVqZmMaUUv4WZlwXze.OTfWki2d2qZEjj01isCZZ...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 22, current_sign_in_at: "2016-03-09 01:06:39", last_sign_in_at: "2016-03-08 19:55:56", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2015-07-25 02:20:15", updated_at: "2016-03-09 01:06:39", first_name: "Marc", confirmation_token: nil, confirmed_at: "2015-07-25 02:20:16", confirmation_sent_at: "2015-07-25 02:20:16", unconfirmed_email: nil, invitation_relation: nil, avatar: "Marc-bio-pic.jpg", invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_id: nil, invited_by_type: nil, invitations_count: 0, bio: "I am Marc Gayle. I build things and I love my fami...", last_name: "Gayle", gender: 0, birthday: nil, storage_used_in_bytes: 0>]
每个User has_many :nodes
,因此当我查询每个nodes
对象的user
时,它会返回nodes
的集合,如下所示:
[21] pry(main)> marc.id
=> 1
[22] pry(main)> marc.nodes
=> [#<Node id: 52, name: "High Knees", family_tree_id: 1, user_id: 1, media_id: 52, media_type: "Video", created_at: "2016-02-17 04:44:11", updated_at: "2016-03-08 22:21:52", circa: nil, is_comment: nil, cached_votes_total: 0, cached_votes_score: 0, cached_votes_up: 0, cached_votes_down: 0, cached_weighted_score: 0, cached_weighted_total: 0, cached_weighted_average: 0.0, cached_user_tag_list: "", cached_num_user_tags: 0, cached_tagged_user_names: [], comments_count: 0, description: "10PP Form Video High Knees">,
#<Node id: 64, name: "Soaked", family_tree_id: 1, user_id: 1, media_id: 64, media_type: "Video", created_at: "2016-03-08 17:59:46", updated_at: "2016-03-08 22:21:54", circa: nil, is_comment: nil, cached_votes_total: 1, cached_votes_score: 1, cached_votes_up: 1, cached_votes_down: 0, cached_weighted_score: 1, cached_weighted_total: 1, cached_weighted_average: 0.0, cached_user_tag_list: "", cached_num_user_tags: 0, cached_tagged_user_names: [], comments_count: 0, description: "10PP Soaked Workout">]
如何根据以上集合获得全面的nodes
集合,如果不手动迭代集合,则没有重复项?
我可以使用joins
或其他一些ActiveRecord方法实现此目的吗?
答案 0 :(得分:1)
您不需要关联:
Node.where(user_id: j.connected_users.map(&:id))