1

在我的 Rails 应用程序中,我有数百万条记录,我需要通过 mysql 使用分页,但我收到此错误“参数数量错误(3 为 2)”。我的 sql 语法

 @tweets=Tweets.paginate_by_sql(sql, :@page, :per_page => @per_page ).all

任何人都请帮我解决这个问题。提前想好,从很长的时间里卡住了

class CoordinatesController < ApplicationController
  def home 
  end
  def paramas(b)

    @b = params[:show]
    return @b

  end
  def coor(latitude,longitude)
    @latitude=0
    @longitude=0
  end

  def query
    @a=Coordinates.where("city= ?",params[:show]) 
    if(params[:show]== a.city) then 
      @latitude= a.latitude
      @longitude=a.longitude
    end
    if(@latitude=0 && @longitude=0) then
      return  @sql="Select * from tweets where tweet_text LIKE '%text%' AND user_loc LIKE 'paramas' order by id desc"
    else if (@latitude!=0 && @longitude!=0) 
           @min_lat = @latitude - 1.0
           @max_lat = @latitude + 1.0
           @min_lng = @longitude - 1.0
           @max_lng = @longitude + 1.0
           return   @sql = "Select * from tweets where tweet_text LIKE '%text%' AND ( ((longitude BETWEEN min_lng and max_lng) AND (latitude BETWEEN min_lat and max_lat)) OR (user_loc LIKE 'paramas') ) order by id desc"
         else
           return   @sql="Select * from  tweets where tweet_text LIKE  '%text%'"
         end    

    end

我的 tweets_controller 代码

class TweetsController < ApplicationController

include CoordinatesHelper
  def search
    render 'tweets/search'
  end


  def index

   # include CoordinatesHelper
    sql=query
    @tweets=Tweets.paginate_by_sql(sql, :@page, :per_page => @per_page ).all
    #render 'tweets/index'
  end
end

@tweets=Tweets.paginate_by_sql(sql, :@page, :per_page => @per_page ).all。我在这一行中遇到错误,任何人都可以帮助我 wat sd error 。提前谢谢

4

1 回答 1

1

paginate_by_sql接受 2 个参数 - SQL 查询和选项哈希。如果要使用 SQL sanitation,则需要将 SQL 查询模板和参数发送到单个数组中,如下所示:

@tweets=Tweets.paginate_by_sql([sql, @page], :per_page => @per_page ).all

编辑

我的坏 - 我认为 @page 是 SQL sanitizer 的参数。

无论如何,您需要通过以下选项发送页面:

@tweets=Tweets.paginate_by_sql(sql, :page => @page, :per_page => @per_page ).all
于 2013-07-24T09:24:40.303 回答