0

Submitter在使用以下代码调用的模型中显示所有记录时,我排除了索引页面上列出的第一行:

submitters_controller.rb:

def index
@submitters = Submitter.where(:school_id =>@current_user.school_id).order('school_id,first_name').search(params[:search])[1..-1]
respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @submitters }
end
end

但是,我有一个需要显示第一行的搜索选项(尤其是当只有一条记录要显示时)。有没有办法在我的 Submitter.rb (Model) 文件中包含特别包含第一行的文件?

提交者.rb:

def self.search(search)
if search
  Submitter.find(:all, :conditions => ['first_name LIKE :search OR last_name LIKE :search', {:search => "%#{search}%"}])
else
  all
end
end

我尝试将 [0..-1] 添加到Submitter.rb模型的末尾,这使我现在可以进行搜索,但第一条记录仍然隐藏。我知道它正在工作,因为当有多个结果时,我可以在列出的第一行之后看到所有结果:

if search
  Submitter.find(:all, :conditions => ['first_name LIKE :search OR last_name LIKE :search', {:search => "%#{search}%"}])[0..-1]
else
4

1 回答 1

0

试试这个

 def self.search(search)
  if search.present?
    search_query = Submitter.where('first_name LIKE :search OR last_name  LIKE:search', {:search => "%#{search}%"})
    if search_query.count > 1
      result = search_query.offset(1)
    else
      result = search_query
    end
  else
    return self.all
  end
  result.all
end
于 2013-08-01T16:45:35.153 回答