地图(安培;:id)的工作,但不拔(:ID) [英] map(&:id) work but not pluck(:id)

查看:129
本文介绍了地图(安培;:id)的工作,但不拔(:ID)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的code一部分,我需要抓住用户的ID,但勇气不抓住他们。只有地图(安培;:ID)能做到这一点。我想知道这是为什么。

In part of my code I need to grab users's id but pluck doesn't grab them. Only map(&:id) can do it. I was wondering why.

我已经写了code一个快速和肮脏块查找的发生

I've wrote a quick and dirty block of code to find what's happen

  def remove_users user_ids
    p users_to_remove.class
    users_to_remove = self.users.where(id: user_ids)
    if users_to_remove.present?
      self.users -= users_to_remove
      self.save

      p users_to_remove.class
      p users_to_remove
      Rails::logger.info "\n#{users_to_remove}\n"
      users_to_remove_map = users_to_remove.map(&:id)
      p users_to_remove_map
      Rails::logger.info "\nmap id: #{users_to_remove_map}\n"
      users_to_remove_pluck = users_to_remove.pluck(:id)
      p users_to_remove_pluck
      Rails::logger.info "\npluck id: #{users_to_remove_pluck}\n"
      #...
    end
    self.user_ids
  end

谁在我的test.log中返回

Who return in my test.log

#<User::ActiveRecord_AssociationRelation:0x007fb0f9c64da8>


map id: [144004]

   (0.3ms)  SELECT "users"."id" FROM "users" INNER JOIN "groups_users" ON "users"."id" = "groups_users"."user_id" WHERE "groups_users"."group_id" = $1 AND "users"."id" IN (144004, 144005)  [["group_id", 235819]]

pluck id: []

和在我的测试

User::ActiveRecord_AssociationRelation
User::ActiveRecord_AssociationRelation
#<ActiveRecord::AssociationRelation [#<User id: 144004, created_at: "2015-08-06 08:55:11", updated_at: "2015-08-06 08:55:11", email: "user_2@test.com", token: "rpf5fqf5bs1ofme6aq66fwtcz", reset_password_token: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, disabled: false, first_name: "John_2", last_name: "Rivers_4", is_owner: false, organisation_id: 235826, encrypted_password: "$2a$04$8ev1j...f6ICL.ezS....", reset_password_sent_at: nil, default_language: nil, uid: nil, api_key: "rmhh...noyn">]>
[144004]
[]

但奇怪的是。我有用户用的ID。 地图可以得到它们。 勇气不是。

The strange thing is. I have user with id. map can get them. pluck not.

我不明白SQL日志也。我怎样才能获得地图编号的结果,而不在SQL日志中的任何选择?缓存?

I don't understand sql log also. How can I get map id result without any select in sql log? Caching ?

推荐答案

这行:

users_to_remove = self.users.where(id: user_ids)

不立即火了SQL查询。它发出时,你需要这些用户的一些细节的要求。它缓存结果在SQL缓存(因此,当同样的要求去重新分贝,它拦截Rails和永远不会到达数据库)。

Doesn't fire off SQL query immediately. It sends the request whenever you need some details of these users. And it caches the result in SQL cache (so when the same request goes to DB again, it intercepted by Rails and never reaches the database).

所以,当你调用:

users_to_remove.map(&:id)

它使用缓存的结果。但是,当你使用

It uses that cached result. But when you use

users_to_remove.pluck(:id)

它重新读取结果,因为实际的SQL查询的不同。随着 #map SELECT * FROM ... ,并用 #pluck SELECT ID FROM ... 。当查询到达数据库,标识不属于自我不再(你以前正确的删除它们),所以他们没有回来。

It re-fetches the result, because the actual SQL query differs. With #map it is SELECT * FROM ..., and with #pluck it's SELECT id FROM.... And when query reaches the database, IDs doesn't belong to 'self' any longer (you deleted them right before that), so they aren't returned.

这篇关于地图(安培;:id)的工作,但不拔(:ID)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆