Created
July 31, 2017 09:12
-
-
Save Irostovsky/1a9401630d1d27bba69af136f115f2e2 to your computer and use it in GitHub Desktop.
Let we have Post(title, body) -> Comments(body). Write the active record query to get all posts, where "query" is into title or body or comment.
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
# Let we have Post(title, body) -> Comments(body). | |
# Write the active record query to get all posts, where "query" is into title or body or comment. | |
class Post < ActiveRecord::Base | |
#title, body | |
has_many :comments | |
scope :search, ->(text) { | |
wildcard_search = "%#{text}%" | |
joins(:comments).where("title ILIKE :search OR body ILIKE :search OR comments.body ILIKE :search", search: wildcard_search) | |
} | |
end | |
class Comment < ActiveRecord::Base | |
#body | |
belongs_to :post | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment