如何搜索两件事并将结果ruby组合在轨道上

时间:2014-09-17 01:47:35

标签: ruby-on-rails ruby ruby-on-rails-3

我正在处理一个包含两个文本字段的搜索表单:位置和关键字。 该位置遍历所有输入的列表并查找位置标记,而关键字查看列表的描述和标题。

输入位置和关键字后,应显示与该位置和关键字匹配的商家信息。 因此,如果我进入旧金山和零售店,则只会出现位于SF且具有描述或标题零售的列表。

我似乎无法将这两个搜索结合起来。

这就是我所拥有的 listing.rb

   class Listing < ActiveRecord::Base
    belongs_to :user
    belongs_to :category 
    default_scope -> { order('created_at DESC') }
    validates :location, presence: true, length: { maximum: 20 }
    validates :title, presence: true, length: { maximum: 20 }
    validates :user_id, presence: true
    validates :description, presence: true

    has_attached_file :resume

def self.search(search)
  query = "%#{search}%"
  if search
    self.where("title like ? or location like ? or description like ?", query, query, query)
  else
    self.all
  end
end

def self.locsearch(search)
  query = "%#{search}%"
  if search
    self.where("location like ? and (title like ? or description like ?)", query, query, query)
  else
    self.all
  end
end

end

Static_pages_controller.rb

  def home
    @listings = Listing.all
    @listings = @listings.paginate(:page => params[:page], :per_page => 10)
  end

home.html.erb

    <%= form_tag findjobs_path, :controller => 'listings', :action => 'search', :method => 'get' do %>
  <%= text_field_tag :location, "location"  %>
  <%= text_field_tag :keyword, "keyword"  %>
  <%= submit_tag "search" %>
</div>
<% end %

listings_controller

    def index
  @listings = Listing.all 
  @listing = Listing.where(id: params[:id]) if params[:id].present?
#this is the simple search
  @listings = @listings.search(params[:search]) if params[:search].present?
  @listings = @listings.paginate(:page => params[:page], :per_page => 10)
#this is the complicated search from the homepage 
  @listings = @listings.locsearch(params[:search]) if params[:search].present?
  @user = User.find_by_name(params[:name]) if params[:name].present?
  @categories = Category.all

end

def search
  @listings = Listing.where("location like ? and (title like ? or description like ?)",params[:location], params[:keyword], params[:keyword])
end
end

当我添加建议的更改时,在heroku上部署,执行搜索并运行heroku日志我得到以下内容

←[36m2014-09-19T23:12:02.140006+00:00 app[web.1]:←[0m   Parameters: {"utf8"=>"??
?", "location"=>"chicago", "keyword"=>"retail", "commit"=>"search"}
←[36m2014-09-19T23:12:02.170814+00:00 app[web.1]:←[0m   Rendered layouts/_header
.html.erb (3.6ms)
←[36m2014-09-19T23:12:02.139998+00:00 app[web.1]:←[0m Processing by ListingsCont
roller#index as HTML
←[36m2014-09-19T23:12:02.166290+00:00 app[web.1]:←[0m   Rendered listings/index.
html.erb within layouts/application (24.0ms)
←[33m2014-09-19T23:12:02.677911+00:00 heroku[router]:←[0m at=info method=GET pat
h="/favicon.ico" host=nightjobs.herokuapp.com request_id=e3a13b6b-faf4-48c6-aad1
-7e0f88ce0a57 fwd="67.188.167.35" dyno=web.1 connect=1ms service=4ms status=304
bytes=133

1 个答案:

答案 0 :(得分:2)

您可以通过以下方式在一个查询中执行此操作:

self.where("location like ? and (title like ? or description like ?)", query, query, query)

查询表格:

<%= form_tag findjobs_path, :controller => 'listings', :action => 'search', :method => 'get' do %>
  <%= text_field_tag :location, "location"  %>
  <%= text_field_tag :keyword, "keyword"  %>
  <%= submit_tag "search" %>
</div>

Static_pages_controller.rb

def search
  @listings = Listing.where("location like ? and (title like ? or description like ?)",params[:location], params[:keyword], params[:keyword])
end