2010-06-10 60 views
2

我使用Searchlogic在數據庫中的許多字段上進行搜索。其中一個字段是:has_may,:through =>關係,並且我無法使其工作。Searchlogic and:has_many,:through =>

下面是該機型的相關部分:

Source.rb:

class Source < ActiveRecord::Base 
    has_many :authorships 
    has_many :authors, :through => :authorships 
end 

Authorship.rb:

class Authorship < ActiveRecord::Base 
    belongs_to :source 
    belongs_to :author 
end 

Author.rb:

class Author < ActiveRecord::Base 
    has_many :authorships 
    has_many :sources, :through => :authorships 
end 

那麼,在我看來,我有:

<% form_for @search do |f| %> 
    <fieldset><legend>Search the Online Medieval Sources Bibliography</legend> 

    <% f.fields_for @search.conditions do |sources| %> 
     <%= sources.hidden_field :live_not_null %> 

     <p> 
     Text Name: 
     <%= sources.text_field :text_name_like, :size => 95 %>  <br /> 
     <% sources.fields_for :author do |authors| %> 
      Medieval Author: 
      <%= authors.text_field :name_like, :size => 90 %> 
     <% end %> <br /> 
     Modern Editor/Translator:  
    <%= sources.text_field :editor_like, :size => 80 %>   <br /> 
     </p> 
    <% end %> 
    </fieldset> 
<p> 
    <%= f.submit "Search" %> 
</p> 
<% end %> 

搜索頁面加載得很好,但打 「提交」 按鈕,給了我以下錯誤:

Searchlogic::Search::UnknownConditionError in SourcesController#index 

The author is not a valid condition. You may only use conditions that map to a named scope 

下面是從SourcesController代碼:

類SourcesController < ApplicationController的

def index 
    query = if params[:search] then 
    params[:search][:hash] 
    end 
    @search = Source.search(query) 
    @sources = @search.all 
end 

這裏是參數:

參數:{「commit」=>「Search」,「search」=> {「hash」=> {「text_name_like」=>「canterbury」,「date_begin_greater_than_or_equal」=>「」,「author」= > {「name_like」=>「」},「editor_like」=>「」,「link_not_blank」=>「0」,「trans_none_not_null」=>「0」,「trans_other_not_null」=>「0」,「trans_english_not_null」= >「0」,「trans_french_not_null」=>「0」,「region_like」=>「」,「live_not_null」=>「」,「app_facsimile_not_null」=>「0」,「date_end_less_than_or_equal」=>「」}, 「=>」「}}

有沒有人有關於這裏發生的事情的任何想法?你需要看到更多的錯誤信息嗎?

非常感謝!

+0

所以,如果錯誤在'SourcesController#index'中,那麼爲什麼不從那裏粘貼代碼,以及從該控制器接收的參數呢? – 2010-06-11 10:25:11

+0

嗨,弗拉德!我已將這些信息添加到我的問題中。謝謝! – Gwendydd 2010-06-22 23:56:42

回答

0

我也遇到過searchlogic上的這個限制。有一個解決方案的'黑客',我得到了。它真的很簡單。 搜索您在連接模型上查找的項目。這會爲您提供具有該搜索的模型的ID。 這有點古怪,但它可以爲您提供最接近的解決方案。 希望這有助於。 :)