Ruby on rails - how to convert array

Viewed 37
(byebug) content_item.parent_id
*** NoMethodError Exception: undefined method `parent_id' for [#<ContentItemRelationship id: "6186d48b-cdc3-4c83-bea1-3db01d614db8", parent_id: "ac123044-730c-42b8-a220-0b38d6ebe20c", content_item_id: "e0734b6d-76e4-4acf-858f-3c91782f2975", order: nil, created_at: "2022-09-19 13:03:50.430928000 +0000", updated_at: "2022-09-19 13:03:50.430928000 +0000">]:Array

why this error is coming please help me. data bellow :

object content_item contain -

[#<ContentItemRelationship id: "6186d48b-cdc3-4c83-bea1-3db01d614db8", parent_id: "ac123044-730c-42b8-a220-0b38d6ebe20c", content_item_id: "e0734b6d-76e4-4acf-858f-3c91782f2975", order: nil, created_at: "2022-09-19 13:03:50.430928000 +0000", updated_at: "2022-09-19 13:03:50.430928000 +0000">]
1 Answers

as @mechnicov highlighted, here content_item is an array that's when you're getting an error for content_item.parent_id. You need to get the value inside that array using either content_item.first.parent_id or content_item.last.parent_id. This way you'd be able to get parent_id for the content item.

Related