Skip to content

Instantly share code, notes, and snippets.

@Irostovsky
Created July 31, 2017 09:12
Show Gist options
  • Save Irostovsky/1a9401630d1d27bba69af136f115f2e2 to your computer and use it in GitHub Desktop.
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.
# 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