All threads / Compare two active record results along just one column

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Compare two active record results along just one column

Michael Victor asked in General

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.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 33,399+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.