All threads / Is it better to create 'duplicate' inverse join records or traverse via ruby/rails?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Is it better to create 'duplicate' inverse join records or traverse via ruby/rails?

Thomas Bush asked in Databases

I am buidling an application for real estate centered around comparing properties. I have a self referential relationship, Comps, which joins two properties and stores some additional data.

Given:
PropertyA.comps => PropertyB
The following must also be true:
PropertyB.comps => PropertyA

I can only think of two routes to accomplish included below

  1. Add create and destroy hooks to automatically add and removed the inverse relationship. When researching, I found a great article on this method. My only reservation is that creating duplicate records and using hooks feels dirty, but my hunch is that this is an exception.

  2. Handle this case with ruby. Load up all the normal and inverse Comp records, normalize and merge. This approach feels like it would be very slow in comparion.

I was hoping that someone with more experience could validate my assumption that option 1 is the optimal direction to move. It feels like an exception to the rule: that using hooks and creating duplicate records makes sense in this case. I appreciate any help or advice you can provide, thanks!

Join the discussion

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

Join 37,344+ 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.