Lets say we have an array containing ids of objects that need to be fetched
This is the ids that need to fetched from another object array
`idvalues=[2,3]`
This is the object array
array_object=[#<CustomPricing id: 2, base_price: 500>, #<CustomPricing id: 3, base_price: 700>, #<CustomPricing id: 4, base_price: 900>, #<CustomPricing id: 2, base_price: 500>]
How can we fetch objects with the id's 2 & 3 and put them in new array. Here also there are multiple records with same id so just want the first record only.
I tried like this
events = idvalues.each {|id_value| array_object.find(id=id_value)}
but this returned the idvalue itself which is [2,3]. How can we achieve this?
idvalues.mapinstead ofeach2 and 3. I just want the first record for each ids. Also if I tryarray_object.find(id=id_value).firstit returns just 2 object with same idfindtofind {|cp| cp.id == id_value}not even sure how this is working for you right now ifarray_objectis actually anArrayidvalues.map {|id| array_object.find { |o| o.id == id} }CustomPrice.distinct.where(id: idvalues)