Reputation: 8954
I have a class called Trial which has_many
results. Now What I want to do is some calculating with differend rows.
So I have an Array with all the collumn names that should be collected. And I want to use the Collect method but Its kind of tricky.
t = Trial.find(<id>)
["collumn1", "collunn2", "collumn3"].each do |collumn_name|
data = t.results.send("collect", &:collumn_name)
# HERE I WANT DO WORK WITH THE COLLECTED DATA
end
But it doesnt work because the collect method excepts some block not String. How can I deal with this Problem?
Upvotes: 0
Views: 77
Reputation: 31726
Symbol#to_proc will give you what you want:
t = Trial.includes(:results).find(<id>) # <-- use includes to avoid having to requery for results
[:collumn1, :collunn2, :collumn3].each do |collumn_name|
data = t.results.collect &collumn_name
# whatevs
end
Upvotes: 1