I have a song.rb model and a genre.rb model that I'd like to be able to search.
For some reason Tire is only returning search results for songs and not genres.
The code:
genre.rb
class Genre < ActiveRecord::Base
has_many :genre_songs, :dependent => :destroy
has_many :songs, through: :genre_songs
include Tire::Model::Search
include Tire::Model::Callbacks
end
song_controller snippit
def index
if params[:query].present?
@songs = Song.search(params[:query], load: true)
elsif params[:genre]
@songs = Song.tagged_with(params[:genre]).paginate(:page => params[:page], :per_page => 15)
get_last_song
else
@songs = Song.order('id').order('plusminus desc nulls last').paginate(:page => params[:page], :per_page => 15)
#@songs = Song.tally.paginate(:page => params[:page], :per_page => 15)
get_last_song
end
end
song.rb snippit
include Tire::Model::Search include Tire::Model::Callbacks
index.html.erb snippit
<%= form_tag songs_path, method: :get do %>
<p>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search", name: nil %>
</p>
<% end %>
in controller:
@songs = Song.search(params)
in song model:
def self.search(params)
tire.search(load: true, page: params[:page], per_page: 15) do
query { string params[:query], default_operator: "AND" } if params[:query].present?
end
end
def to_indexed_json
to_json(methods: [:genre_names])
end
def genre_names
genres.map{ |g| g.name}
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.