是否可以将 will_paginate 与 find_by_sql 结合起来?

发布于 2024-08-26 12:18:04 字数 524 浏览 10 评论 0原文

在我的 Rails 应用程序中,我想使用 will_paginate gem 对我的 SQL 查询进行分页。这可能吗?我尝试做这样的事情,但没有成功:

@users = User.find_by_sql("
    SELECT u.id, u.first_name, u.last_name, 
     CASE 
      WHEN r.user_accepted =1 AND (r.friend_accepted =0 || r.friend_accepted IS NULL)
       .........").paginate(
                  :page => @page, :per_page => @per_page, 
                  :conditions => conditions_hash,
                  :order => 'first_name ASC')

如果没有,你能推荐一种解决方法吗?我不想编写自己的分页。

In my Rails application I want to use the will_paginate gem to paginate on my SQL query. Is that possible? I tried doing something like this but it didn't work:

@users = User.find_by_sql("
    SELECT u.id, u.first_name, u.last_name, 
     CASE 
      WHEN r.user_accepted =1 AND (r.friend_accepted =0 || r.friend_accepted IS NULL)
       .........").paginate(
                  :page => @page, :per_page => @per_page, 
                  :conditions => conditions_hash,
                  :order => 'first_name ASC')

If not, can you recommend a way around this? I don't want to have to write my own pagination.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北斗星光 2024-09-02 12:18:05

试试这个:

@users = User.find_by_sql 
@users = @users.paginate

您使用的 will_paginate 和 Rails 版本是什么?

Try this:

@users = User.find_by_sql 
@users = @users.paginate

What will_paginate and rails version are you using?

偏爱你一生 2024-09-02 12:18:04

使用paginate_by_sql,即

sql = " SELECT * 
        FROM   users
        WHERE  created_at >= ?
        ORDER  BY created_at DESC "

@users = User.paginate_by_sql(
  [sql, 2.weeks.ago],
  page: @page,
  per_page: @per_page
)

作为一般规则,任何查找器都可以通过将find* 替换为paginate* 来进行分页。

Use paginate_by_sql, i.e.

sql = " SELECT * 
        FROM   users
        WHERE  created_at >= ?
        ORDER  BY created_at DESC "

@users = User.paginate_by_sql(
  [sql, 2.weeks.ago],
  page: @page,
  per_page: @per_page
)

As a general rule any finder can be paginated by replacing the find* with paginate*.

难以启齿的温柔 2024-09-02 12:18:04

User.paginate_by_sql(sql, :page => params[:page], :per_page => 10)

User.paginate_by_sql(sql, :page => params[:page], :per_page => 10)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文