Reputation: 31074
I'm a fan of Hibernate's HQL syntax, that lets me form queries that traverse model relationships, like:
from Book book where book.publisher.lastName = 'jones'
from Book book where book.chapters.length = 2
Is there an ActiveRecord syntax for traversing models like this?
Upvotes: 2
Views: 226
Reputation: 9700
I think the closest you're going with to get with ARel is:
Book.joins(:publisher).where('publishers.lastName = ?', 'Jones')
or
Book.where(:publisher_id => Publisher.where(:last_name => 'Jones'))
(which is probably less efficient, as it will being hitting the database twice instead of once)
and
Book.joins(:chapters).group('books.id').having('count(chapters) = ?', 2)
Which I admit is a bit less readable.
There are some gems, Squeel is the one that comes to mind, which let you change up your query syntax in various ways - it's possible one of them will feel closer to what you want to use.
Upvotes: 1