从多个表中检索过滤数据
https://guides.rubyonrails.org/active_record_querying.html
从多个表中检索过滤数据
Customer<br />
.select('customers.id, customers.last_name, reviews.body')<br />
.joins(:reviews)<br />
.where('reviews.created_at > ?', 1.week.ago)查询语句执行的结果是这样的:
SELECT customers.id, customers.last_name, reviews.body<br />
FROM customers<br />
INNER JOIN reviews<br />
ON reviews.customer_id = customers.id<br />
WHERE (reviews.created_at > '2019-01-08')从多个表中检索特定数据
Book<br />
.select('books.id, books.title, authors.first_name')<br />
.joins(:author)<br />
.find_by(title: 'Abstraction and Specification in Program Development')查询结果:
SELECT books.id, books.title, authors.first_name<br />
FROM books<br />
INNER JOIN authors<br />
ON authors.id = books.author_id<br />
WHERE books.title = $1 [["title", "Abstraction and Specification in Program Development"]]<br />
LIMIT 1