开发者

What am I doing wrong in this paginate_by_sql call?

Forgive me, guys; I'm still a complete RoR newbie.

I'm trying to use the will_paginate gem to add pagination to search results in my Rails app. Thus far it's been working great. However, I've hit a roadblock.

I have a "products" table with thousands of records that I want to be completely browsable. This is easy in the most basic scenario. I've got this in my controller:

page = params[:page] || 1
@products = Product.paginate :page => page, :order => 'symbol'

What I want to do is add a parameter, "letter," so that only products whose symbols start with the given letter are paginated and displayed.

This is what I tried:

letter = params[:letter] || 'A'
page = params[:page] || 1
@products = Product.paginate_by_sql
    ['select * from products where symbol like ?', letter + '*'],
    :page => page
    :order => 'symbol'

But with this code in place, the page won't load. I see this in the log file, but it doesn't actually look like an error to me:

Processing ProductsController#index (for 127.0.0.1 at 2010-09-15 10:03:22) [GET]
  [4;36;1mProduct Load (9.0ms)[0m   [0;1mselect * from products where symbol like 'A*' LIMIT 50 OFFSET 0[0m
Rendering temp开发者_运维知识库late within layouts/main
Rendering products/index
Completed in 21ms (View: 4, DB: 9) | 200 OK [http://localhost/products]

I realize I'm probably missing something obvious, or else not including the information that would be needed to solve this problem. Would anyone care to steer me in the right direction, or let me know what other info I should provide?


Not sure what database you are using, but I believe your SQL is wrong. Shouldn't this be letter + '%' not letter + '*'?

% is generally the SQL wildcard.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜