It’s giving me a hard time to debug this. It was working perfectly before I moved to PostgreSQL from SQLite3. What’s strange to me is that there’s another controller that has almost the same code as this, and that page works fine. Could anyone help me?
Error:
ActiveRecord::StatementInvalid in Good_posts#index
Showing c:/ruby/mangfeel/app/views/good_posts/index.html.erb where line #3 raised:
PG::Error: ERROR: syntax error at or near "desc"
LINE 1: ... "good_posts".* FROM "good_posts" ORDER BY like desc LIMIT...
^
: SELECT "good_posts".* FROM "good_posts" ORDER BY like desc LIMIT 15 OFFSET 0
good_posts_controller that’s causing the problem:
def index
@good_posts = GoodPost.paginate(:page => params[:page], order: 'like desc', per_page: 15)
@good_client_ip = request.remote_ip.encode! 'utf-8'
respond_to do |format|
format.html # index.html.erb
format.json { render json: @good_posts }
end
end
post_controller, which has similar code but works fine:
def index
@posts = Post.paginate(:page => params[:page], order: 'created_at desc', per_page: 15)
@client_ip = request.remote_ip.encode! 'utf-8'
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
I appreciate all the help!
LIKE is an operator so you can’t use it as a column name unless you double quote it:
Whenever ActiveRecord generates a column name in some SQL it will quote the column name for you; so
.where(:like => 11)will be sent to the database aswhere "like" = 11and everyone is happy. When you write the column name in an SQL snippet (such as.where('"like" = ?', 11)) you’ll have to double quote it to distinguish it from the pattern matching operator. You’ll be happier if you rename the column to something (such aslikesornumber_of_likes) that doesn’t conflict with a keyword