Come passare metodi a Ruby Map
Oct 09 2020
Ho il codice seguente:
wedding = Wedding.where(location_id: user_params[:locationId])
wedding.map(&:guests).each do |member|
user_ids << member.ids
end
Nel mio caso :guestsè una tabella di record attiva, ma ne ho un paio che vorrei passare attraverso mapper generare il fileuser_ids
Quindi sarebbe una serie di metodi come questo, che vorrei passare: [guests, bride, etc etc]
Sarebbe anche meglio se potessi passare l'intero array, ma altrimenti se potessi passare attraverso l'array di metodi sarebbe fantastico.
Qualche idea?
EDIT: lo sto provando senza fortuna .. ottengo: NameError (wrong constant name guests):
roles = ["guests"]
wedding = Wedding.where(location_id: user_params[:locationId])
roles.each do |role|
clazz = Kernel.const_get(role)
wedding.map(&:clazz).each do |member|
user_ids << member.ids
end
end
Risposte
2 PhiAgent Oct 09 2020 at 17:46
Di seguito, passo una serie di metodi ai membri dell'array weddings:
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