【问题标题】:Retrieving data in a hash在哈希中检索数据
【发布时间】:2021-05-07 09:47:48
【问题描述】:

我需要从 ActiveRecord 获取一些数据,我有以下两个表 Department 和 users 我有一个问题,我得到一个哈希,其中用户给我 user_ids 和电子邮件,现在我想创建哈希容器用户、部门和特定格式的电子邮件。我已经尝试了很多地图/选择,但仍然无法找到任何简单的方法。

class User < ApplicationRecord
  belongs_to :department
end

和部门

class Department < ApplicationRecord
  has_many :users
end

我从用户那里得到以下值

sample_params = [{ user_id: 1, email: 'example1@example.com' }, 
{ user_id: 5, email: 'example5@example.com' }, 
{ user_id: 13, email: 'example13@example.com'}]

现在我已经从数据库和其他数据中检索部门并加入一个巨大的哈希,以便我可以将它添加到我的班级。我可以通过以下命令获取所有用户

users = User.where(id: sample_params.map{ |m| m[:user_id] })

如果我运行以下命令,我将获得所有用户的整个 ActiveRecord 对象,我将获得所有 user_id 和 project_id

users.map { |u| {user_id: u.id, department_id: u.department_id } }

我会得到这个

[{:user_id=>1, :department_id=>3}, 
{:user_id=>5, :department_id=>3}, 
{:user_id=>13, :department_id=>2}]

但我想创建以下哈希,有没有简单的方法可以直接使用查询或其他几行来完成,我可以尝试使用迭代,但这会很长而且很复杂。因为我还需要在其中合并电子邮件并添加一个项目而不是同一个项目多个 id。

[
{department_id: 1, users: [{user_id: 1, email: 'example1@example.com'},
 {user_id: 5, email: 'example5@example.com'}]}, 
{department_id: 2, users: [{ user_id: 13, email: 'example13@example.com']

我这里使用的是样本数据,实际数据非常非常大,包括数百个用户和许多部门。

【问题讨论】:

    标签: ruby-on-rails activerecord


    【解决方案1】:

    我认为你不能一口气完成!让我们通过并尝试如何解决它。首先,不要在您的参数中使用 map ,而是让我们尝试另一种替代方法。删除以下行

    users = User.where(id: sample_params.map{ |m| m[:user_id] })
    

    用户跟随行

    User.where(id: sample_params.pluck(:user_id)).pluck(:department_id, :id).grou
    p_by(&:first)
    

    这将在一个查询中为您带来每个用户 ID 和部门分组的结果

    {3=>[[3, 1], [3, 5]], 2=>[[2, 13]]}
    

    现在我们正在获取 department_id 和 user_id,因此我们将在它们上运行 map 以使用以下命令在组中获取第一个具有 department_id 和 user_id 的数组

    data =
      User
      .where(id: sample_params.pluck(:user_id))
      .pluck(:department_id, :id)
      .group_by(&:first).map do |department_id, users|
        { department_id: department_id,
          users: users.map { |_, id| id } }
      end
    
    

    这将为您提供部门和用户的哈希值

    [{:department_id=>3, :users=>[1, 5]}, {:department_id=>2, :users=>[13]}]
    

    现在您有了部门和用户的哈希值。所以让我们来解决这个问题,这是第二阶段,我将使用select 从您的参数中获取电子邮件并将其合并到您的数据中。

    result = []
    data.each do |department|
      department_users = []
       department[:users].each do |user|
         emails = sample_params.select { |user| user[:user_id] == 1 }[0][:email];
         department_users << { id: user, emails: emails }
       end; result << {department_id: department[:department_id], users: department_users}
    end
    

    现在,如果您执行puts result[0],您将获得所需的以下哈希值。

    {:department_id=>3, :users=>[{:id=>1, :emails=>"example1@example.com"}, {:id=>5, :emails=>"example1@example.com"}]}, {:department_id=>2, :users=>[{:id=>13, :emails=>"example1@example.com"}]}
    

    这将解决问题,我知道有两个操作,但在单个操作中没有双 sql 查询,它正在工作并且您也在替换您的电子邮件。我希望它能解决你的问题,任何人都可以使它更简单。

    【讨论】:

    • 嗯,它解决了问题,但它是很多代码!
    猜你喜欢
    • 2017-06-10
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    相关资源
    最近更新 更多