Module: Course::SearchConcern::ClassMethods

Defined in:
app/models/concerns/course/search_concern.rb

Instance Method Summary collapse

Instance Method Details

#search(keyword) ⇒ Array<Course>

Search and filter courses by their titles, descriptions or user names.

Parameters:

  • keyword (String)

    The keywords for filtering courses.

Returns:

  • (Array<Course>)

    The courses which match the keyword. All courses will be returned if keyword is blank.


10
11
12
13
14
15
16
17
# File 'app/models/concerns/course/search_concern.rb', line 10

def search(keyword)
  return all if keyword.blank?

  condition = "%#{keyword}%"
  joining { users.outer }.
    where.has { (title =~ condition) | (description =~ condition) | (users.name =~ condition) }.
    group('courses.id')
end