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.

Reply
Join the discussion
Create an account Log in

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

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

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