Last active
July 20, 2019 13:31
-
-
Save kaineer/631760 to your computer and use it in GitHub Desktop.
SQL-запросы (ActiveRecord)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Book < ActiveRecord::Base | |
has_many :links, :class_name => "BookAuthorLink" | |
has_many :authors, :class_name => "Author", :through => :links, :source => :authors | |
# Получение списка книг, не имеющих авторов | |
def self.orphant_books | |
self.find( :all, | |
:joins => "LEFT OUTER JOIN book_author_links AS l ON l.book_id = books.id", | |
:conditions => "l.id IS NULL" ) | |
# Решение на чистом ActiveRecord предполагает, если я правильно понимаю, | |
# проверку наличия авторов в цикле по всем книгам. Что выглядит довольно неприятно. | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Book.orphant_books # -> книги без автора | |
Author.withoub_books # -> авторы, по которым нет книг |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment