I am just learning rails. I like so much about Rails. It utterly destroys Java and related solutions for productivity in almost all cases – and I am really freaking fast at Java having over a decade experience… so that says something! If rails doesn’t do it, there’s a gem out there that blows my mind away how productive it is.
Then there’s Active Relation. I am trying really hard to use this as my main query approach, but it’s a no-go. I find that once I start building queries that require 2 joins, I start getting weird error messages that don’t make sense… and honestly, all the scopes can be hard to reason about.
While this approach might make things a bit more readable, I think the cost in reasoning what’s going on from a big picture perspective… and the cost of writing the query… probably doesn’t pay for itself. If you are really good at sql… or a language like hql… this way of doing things is actually a productivity loss. At least it is for me.
I could live with it, but I can’t get a nested join to work. For example if I have A<->B<->C, and B is a model that is also a Many-to-Many relationship, ActiveRelation complains that the name of one of the foreign keys is wrong… even though it’s actually correct. I think it’s a bug. All the queries that do single joins using scopes work flawlessly… but if you combine those very same scopes to make 2 joins, it fails. I see no way to fix it.
I could do just one of the joins, and then let the ORM do the rest for me… but that’s going to be such a waste of performance. That’s going to end in disaster for a big site. I am writing a massive system that stores massive trees of relational data by the thousands per request. Traversing the graph using the ORM is just too much of a performance killer that I won’t even bother continue to code the rest of the app. I’ll probably have to go back to Java if this is the case.
On top of this, I can’t find any examples of multiple joins on the net. Even the documentation hardly mentions joins and merges at all.
So… is it ready for prime time? How does one do really complex 7-9 table joins in Rails? Is Rails just not built for this? I only ask this because if Rails can’t handle this type of querying, I will unfortunately have to stop using it… and trust me… that’s a massive, real shame… because I absolutely LOVE everything else. I really hope I am just wrong and everything is fine.
To tell you the truth, i can relate a bit, but the truth is that we most times do not know how to handle things and get frustrated. Rails, with its steep learning curve, is a great example of that 🙂 I was trying to do a left outer join one time and it looked so messy, only to find out that i could do it in a cleaner way.
Rails can actually do all sorts of joins, once you learn how to instruct it (and that can indeed be a challenge, based on my experience).
What i found out is that using a bit of Ruby at times, makes the procedure easier. A little map might just save the whole thing.
However, when you can’t do something, it’s not because Rails does not handle it, but because you do not have the knowledge to do it properly yet. Rails is hard and some things seem very unclear, just because you don’t know about them at first.
About the A,B,C thing, it should work fine. It’s highly probably(if not certain), that you are doing something wrong on naming your keys(you can be 100% sure that it’s not a Rails bug 🙂