Ruby on rails 如何将方法传递给Ruby Map

Ruby on rails 如何将方法传递给Ruby Map,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我有以下代码: wedding = Wedding.where(location_id: user_params[:locationId]) wedding.map(&:guests).each do |member| user_ids << member.ids end 下面,我将向数组成员传递一个方法数组: weddings = Wedding.where(location_id: user_params[:locationId]) # array with me

我有以下代码:

wedding = Wedding.where(location_id: user_params[:locationId])
wedding.map(&:guests).each do |member|
  user_ids << member.ids
end

下面,我将向数组成员传递一个方法数组:

weddings = Wedding.where(location_id: user_params[:locationId])

# array with methods you're interested in
methods=[:guests, :bride]

# looping through the weddings array
weddings.each do |wedding|
  
#   looping through the methods array
  methods.each do |method|
    
#     for each wedding, passing every method to the wedding
    members=wedding.public_send(method)
    members.each do |member|
      
#       storing the values
      user_ids << member.ids
    end
  end
end


weddings=Wedding.where(location\u id:user\u params[:locationId])
#包含您感兴趣的方法的数组
方法=[:来宾,:新娘]
#在婚礼阵列中循环
婚礼。每个人都有婚礼|
#循环遍历方法数组
方法。每个do |方法|
#对于每一场婚礼,将每一种方法传递给婚礼
成员=婚礼。公开发送(方法)
成员。每个成员|
#存储值

用户ID您是否尝试了.send方法?请检查我的答案,看看这是否适用于您。您可以更好地解释您的模式吗?你想在一个婚礼地点收集所有的客人(不管实际的婚礼是什么?),所以你最终会有来自不同婚礼的多个新娘/新郎,所有的客人都是这样想的?最好使用
#public\u send
而不是
#send
:)哦,我明白了,为什么?这是一个很好的理由:)哦,我明白了,所以公共发送是为了保存封装,而发送绕过了封装。谢谢@AlexGolubenko。另外,我不推荐使用或作为局部变量名,因为这两个名称实际上都是“方法”。此外,映射过程是密集的,以获得所需的结果。
weddings = Wedding.where(location_id: user_params[:locationId])

# array with methods you're interested in
methods=[:guests, :bride]

# looping through the weddings array
weddings.each do |wedding|
  
#   looping through the methods array
  methods.each do |method|
    
#     for each wedding, passing every method to the wedding
    members=wedding.public_send(method)
    members.each do |member|
      
#       storing the values
      user_ids << member.ids
    end
  end
end