add word list page and search filter

I've implement word list page and filter please review.
lesson_word_ids = LessonWord.select("word_id").where(user_id: user_id).to_sql
if learned  
  sql = Word.where 
  if category
    Word.where("id in #{lesson_word_ids} and category_id = ?, category_id)
  else
    Word.where("id in #{lesson_word_ids}")
  end
else
  if category
    Word.where("id not in #{lesson_word_ids} and category_id = ?, category_id)
  else
    Word.where("id not in #{lesson_word_ids}")
  end