2014-01-24 33 views
0

我有兩個表。 這裏是我的數據模式。如何在使用輪胎彈性搜索的活動記錄關係中搜索。(has_many,belongs_to)

create_table "comments", force: true do |t| 
    t.integer "movie_id" 
    t.string "user_comment" 
    t.string "user" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "movies", force: true do |t| 
    t.string "title" 
    t.integer "year" 
    t.text  "cast" 
    t.string "director" 
    t.string "category" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "like" 
    t.integer "dislike" 
    t.string "genre" 
    t.integer "rating" 
    end 

end 

我有兩個型號,電影&評論 型號/ movie.rb

class Movie < ActiveRecord::Base 
    has_many :comments 
    include Tire::Model::Search 
    include Tire::Model::Callbacks 
    after_touch() { tire.update_index } 
    mapping do 
     indexes :title,boost: 100 
     indexes :year,:analyzer => 'snowball' 
     indexes :cast,:analyzer => 'snowball' 
     indexes :user_comment 
    end 

    def self.search(params) 
     tire.search(load: true) do |s| 
      s.query {string "*#{params[:query]}*"} if params[:query].present? 
      s.filter :range,year: {lte: 2004} 
      end 
    end 

    def to_indexed_json 
    to_json(methods: [:user_comment]) 
    end 

    def user_comment 
    movie.comments.user_comment 
    end 
end 

型號/ comment.rb

class Comment < ActiveRecord::Base 
    belongs_to :movie,touch: true 
end 

一個搜索控制器來搜索查詢。 控制器/ search_controller.rb

class SearchController < ApplicationController 
    def index 
    if(params[:query]).present? 
    @results=Movie.search(params) 
    else 
    @results=[] 
    end 
    end 
end 

的意見/搜索/ index.html.erb

<h1>Search#index</h1> 
<p>Find me in app/views/search/index.html.erb</p> 


    <%= form_tag search_index_path, method: :get do %> 
     <p> 
     <%= text_field_tag :query, params[:query] %> 
     <%= submit_tag "Search", name: nil %> 
     </p> 
    <% end %> 
    <% if @results %> 
    <% @results.each do |fetchresults| %> 
    <%= fetchresults.title %> 
    <%= fetchresults.year %> 
    <%= fetchresults.director %> 

    <br/> 
    <% end %> 
    <% end %> 

我試圖尋找使用輪胎的意見,在電影控制器。 當我試圖運行命令: - 耙環境輪胎:進口CLASS =「電影」 FORCE =「真」

它顯示了一個錯誤: -

[IMPORT] Deleting index 'movies' 
[IMPORT] Creating index 'movies' with mapping: 
{"movie":{"properties":{"title":{"boost":100,"type":"string"},"year":{"analyzer":"snowball","type":"string"},"cast":{"analyzer":"snowball","type":"string"},"user_comment":{"type":"string"}}}} 
rake aborted! 5% |||          | ETA: 00:00:03 
undefined local variable or method `movie' for #<Movie:0x0000000337c5c8> 
/home/user/Desktop/abc/elasticsearch/elastictest/app/models/movie.rb:36:in `user_comment' 
/home/user/Desktop/abc/elasticsearch/elastictest/app/models/movie.rb:32:in `to_indexed_json' 
Tasks: TOP => tire:import => tire:import:model 
(See full trace by running task with --trace) 

誰能幫助我。 我的任務是從評論表中搜索評論。 &還有一件事情,在我看來,如何獲得該評論的電影ID。 謝謝。

回答

0

好像問題是你的方法

def user_comment 
    movie.comments.user_comment 
    end 

應該

def user_comment 
    comments.user_comment 
    end 
相關問題