I need to check, if for each item in @line_items if it is in different array say @quote_items
Controller:
def index
@line_items = LineItem.all
@quote_items = QuoteItem.all
end
View:
<% for line_item in @line_items %>
<% if @quote_items.include?(line_item) %>
line_item in quote item!
<% else %>
line_item NOT in quote item!
<% end %>
...
<% end %>
Is there an easy way to do this? include? doesn’t seem to work the way I want. Seems to just yield false for me all the the time.
You are right it will always return false because you are trying to check if the array of @quote_items has a line item object
which obviously will always be false because your @quote_items instance is an array of QuoteItem objects and @line_items instance is an array of LineItem object. So they are always different objects.
I think in this situation you may want to compare some common attribute of quote_item and line_item. For example if you want to compare name attribute then
and then