Ruby’s Enumerable has a select which can select certain items from an array:
parent=@sections.select { |p| p.id==i.parent_id }
There is also a .find method, to select just the first match. However, parent=@sections.find { |p| p.id==i.parent_id } calls the ActiveRecord method instead of the Enumerable one. How do I use the Enumerable .find method?
But why would you want to grab all of the records from the database and then perform a ruby find on them, when it is far far far more efficient to let the database do use it’s relational magic and get the correct records for you based on the id’s?
Getting all of the records and then doing a ruby find is not scalable at all – I learned this very quickly in my first database-driven applications when I would grab every single record and loop through them to collect the data I wanted. It’s no good at all.
You should just do
i.parentassumingi belongs_to :parents