Reputation: 5349
I'm trying to perform this non-basic query with Rails :
proj_repartitions = ProjRepartition.includes(:proj_contributions).find( proj_project.proj_charges.each{|pc| pc.proj_repartition_id} )
The point is I need to pass an array of 'ids' to the find
method.
I want to extract those 'ids' from an array of ActiveRecord objects where each of these is equipped with an 'id' attribute.
When I try on the Rails console:
proj_project.proj_charges.each{|pc| pc.proj_repartition_id}
I got exactly the same array as proj_project.proj_charges
What am I doing wrong?
=== UPDATE ===
According to the answers, my definitive working code is:
proj_project.proj_charges.collect{|pc| pc.proj_repartition_id}
Upvotes: 0
Views: 164
Reputation: 20868
proj_project.proj_charges.map(&:proj_repartition_id)
Short way of writing
proj_project.proj_charges.map{|pc| pc.proj_repartition_id}
Upvotes: 1
Reputation: 696
Each only executes the do block code for each item but doesn't "save" the return value.
Collect and Map executes the do block code for each item and the return value in an array.
hope that makes sense
Upvotes: 1
Reputation: 11188
Instead of using each try map or collect - they should return an array.
Upvotes: 1