anykeyh/clear

order_by not working as described in documentation

dansbits opened this issue · 0 comments

Hello! I'm pretty new to Crystal and Clear so sorry if I'm missing something obvious.

The gitbook documentation gives the following example for how to order query results.

User.query.order_by(last_name: "ASC", first_name: "ASC").each do |usr|
    puts "#{usr.first_name} #{usr.last_name}"
end

I have a model called Post with a created_at column. Based on that example, if I run posts = Post.query.order_by({ created_at: "desc" }).limit(20) I'm getting the following error.

In lib/clear/src/clear/sql/query/order_by.cr:84:9

84 | order_by(k, v, nil)
       ^-------
Error: no overload matches 'Post::Collection#order_by' with types Symbol, String, Nil

Overloads are:
- Clear::SQL::Query::OrderBy#order_by(x : Array(Record))
- Clear::SQL::Query::OrderBy#order_by(__tuple : NamedTuple)
- Clear::SQL::Query::OrderBy#order_by(expression : Symbol, direction : Symbol = :asc, nulls : Symbol | ::Nil = nil)
- Clear::SQL::Query::OrderBy#order_by(expression : String, direction : Symbol = :asc, nulls : Symbol | ::Nil = nil)
- Clear::SQL::Query::OrderBy#order_by(**tuple)

Based on the proposed overloads in the error message I changed my code to posts = Post.query.order_by(:created_at, :desc).limit(20) to get a working result but I can't figure out why the original doesn't work. The error lists order_by(__tuple : NamedTuple) as a supported overload.

My dependencies and crystal version are below:

dependencies:
  kemal:
    github: kemalcr/kemal
  pg:
    github: will/crystal-pg
    version: 0.23.2
  clear:
    github: anykeyh/clear
    branch: master

crystal: 1.3.2