ruby - Rails using uniq_by method with nested records -


i have collectionproxy of nested records:

@saved_videos = video.all.is_saved @sentences_to_extract = @saved_videos.map(&:sentences) 

each video has many sentences, , each sentence has 1 keyword, eg

[   [video.id: 1,      sentences:       [sentence.id: 1, keyword: 'hello'],       [sentence.id: 2, keyword: 'friend']     ],   [video.id: 2,      sentences:       [sentence.id: 3, keyword: 'hello'],       [sentence.id: 4, keyword: 'mum']     ] ] 

when try:

@sentences_to_extract.uniq_by(&:keyword) 

it returns

undefined method `uniq_by' #<array:0x000001031f7968>  

what best way return single collection of sentences unique keywords, or in more general terms, selection of nested records particular unique attribute?

with @aruprakshit solved: need flatten mapped arrays 1 level, achieved in 1 liner flat_map.

working code:

video.all      .is_saved.flat_map(&:sentences)      .uniq { |s| s.keyword.downcase } 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -