我有 2 个模型,Post 和 Location,其中 location has_many posts 和 post owns_to location。搜索工作正常,分页也工作正常,除了total_entries。结果显示超过 10 条
查看search.html:
<%= form_tag search_posts_path, :method => 'get' do %>
<p>
<%= text_field_tag :title, params[:title] %>
<%= text_field_tag :company, params[:company] %>
<%= select_tag :location_id, options_from_collection_for_select(Location.all, :id, :name, params[:location_id]), include_blank: true %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
控制器post_controller.rb:
def search
title = params[:title]
company = params[:company]
location_id = params[:location_id]
@posts = Post.search(title, company, location_id)
end
模型帖子.rb
def self.search(title, company, location_id)
if location_id.present?
paginate :conditions => ['title LIKE ? AND company LIKE ? AND location_id = ?', "%#{title}%", "%#{company}%", location_id],
:per_page => 20,
:order => 'created_at DESC',
:page => @page,
:total_entries => 10
else
paginate :conditions => ['title LIKE ? AND company LIKE ?', "%#{title}%", "%#{company}%"],
:per_page => 20,
:order => 'created_at DESC',
:page => @page,
:total_entries => 10
end
end