2014-12-21 5 views
0

반복되는 개체보다 연관된 개체가있는 컬렉션에서 첫 번째 개체를 찾는 방법이나 더 좋은 방법이 있습니까? break?관계가있는 첫 번째 개체 찾기

collection.each {|item| break item if item.the_associations.present?} 

답변

1

'find' for enumerable :

collection.to_a.find{ |i| i.associations.present? } 

아니면 협회에 가입하고 처음으로, 예를 들어 걸릴 수 있습니다

People.joins(:children).first 
관련 문제