Joins
Performing Inner Table Joins
NoBrainer allows inner table joins on associations with join().
Suppose we have posts and comments:
class Author
include NoBrainer::Document
has_many :posts
end
class Post
include NoBrainer::Document
belongs_to :author
has_many :comments
end
class Comment
include NoBrainer::Document
belongs_to :post
endjoin() accepts arrays and hashes to describe the associations to join,
similarly to eager_load().
Examples
The first example shows a table join on a belongs_to association between posts
and authors:
Post.join(:author).each do |post|
author = post.author # access the post's author
endjoin() performs an inner join on the provided associations, the query
iterates on posts that have an author.
The second example shows two joins on has_many associations.
Author.join(:posts => :comments).each do |author|
post = author.posts.first # access the post
comment = post.comments.first # access the comment
endThe query provides one entry per author, per post and per comment, generating
lots of objects. To avoid this, use eager_load() described in the next
section.