Ask A Question


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 =
- @item.property_maps.wood_only.each do |property|
    - item_array.push(property.property_id)
- customization_array =
- 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 = {|property| "#{}"}.join(', ')
- elsif @item.property_maps.wood_only.present?
                td = {|property| "#{}"}.join(', ')
            - else 

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
Create an account Log in

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

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

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