Skip to main content

Compare two active record results along just one column

General • Asked by Michael Victor

I have 3 models - Items, Customizations and Property Maps

Property Maps has a polymorphic association with Items and Customizations

The business logic here is, when listing customizations properties -

If the customization does not have any properties, display the item properties in a normal cell
If the customization has properties that are different from the item, display the list of properties in a "customized-cell" class
If the customization has the same properties as that of the item, display the list of properties in a normal cell.
If both, the customization and item have no customization, display a blank cell.
This is my current implementation of the above

- item_array = Array.new
- @item.property_maps.wood_only.each do |property|
    - item_array.push(property.property_id)
- customization_array = Array.new
- customization.property_maps.wood_only.each do ||
    - customization_array.push(property.property_id)
- if customization.property_maps.wood_only.present? && item_array.sort != customization_array.sort
    td.customized-cell = customization.property_maps.wood_only.map {|property| "#{property.property.name}"}.join(', ')
- elsif @item.property_maps.wood_only.present?
                td = @item.property_maps.wood_only.map {|property| "#{property.property.name}"}.join(', ')
            - else 
                td`

Is there a way where I can compare the results across a column directly from Active Record? This would save the need for me create new arrays and then compare them.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.