Вопрос

I'm just starting to learn ROR and I need help.

I have two tables and I need to search them using Sunspot solr. The search may look for things such as the company and the headings, so, how do I organize a search for two tables using sunspot solr (ROR)?

section.rb

class Section < ActiveRecord::Base
  has_many :headings
  searchable do
    text :name
  end
end

heading.rb

class Heading < ActiveRecord::Base
  belongs_to :section
  searchable do
    text :name
    text :address
    text :phone
  end
end

Headings Controller

def index
  @sections = Section.all
  @headings = Heading.all
  @search = Sunspot.search(Heading, Section) do
    fulltext params[:search]
  end
  @headsear = @search.results
end

def show
  @se = Section.all
  @sections = Section.all
  @headings = Heading.find(params[:id])
  @sections = Section.find(params[:id])
end

headings/_search.html.erb

<%= form_tag headings_path, :method => :get do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :name => nil %>
  </p>
<% end %>
Это было полезно?

Решение

In your variable @headsear you have both : Heading objects and Section objects If you want these results into 2 variables, you can call search method twice :

@headings = Heading.search {fulltext params[:search]}
@sections = Section.search {fulltext params[:search]}

Another solution is to search only Heading and used the relation with Section :

You need to index section data into Heading class like this : heading.rb

class Heading < ActiveRecord::Base
  belongs_to :section
  searchable do
    text :name
    text :address
    text :phone
    text(:section_name) { section.name }
  end
end

After this when name of a section match with search param, the result will contain all heading object related

Hope this help.

Другие советы

Sunspot.search Heading, Section do
  fulltext params[:search]
end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top