Compare object attributes with array of objects Ruby on Rails
I have an object that I want to compare with an array of objects. If two specific attributes are equal I want to stop the loop. How can I do that, or how can I do this in some better Rails way?
@item #item to compare with @items.each do |item| if ( (item.att1 == @item.att1) && (item.att3 == @item.att3) ) is_equal(item.id) else #do something end end
Answers
Use the find method of an Array:
matched_item = @items.find { | item | item.att1 == @item.att1 && item.att1 == @item.att1 } is_equal(matched_item.id)