2

我正在使用 Searchlogic 搜索数据库中的许多字段。其中一个字段是 :has_may, :through => 关系,我无法让它工作。

以下是模型的相关部分:

来源.rb:

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

作者身份.rb:

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

作者.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"=>""}, "order"=>"" }}

有人对这里发生的事情有任何想法吗?您需要查看更多错误消息吗?

非常感谢!

4

1 回答 1

0

我也遇到了搜索逻辑的这种限制。我得到了一个解决方案的“破解”。它真的很简单。在连接模型上搜索您要查找的项目。这将为您提供具有该搜索的模型的 ID。这有点古怪,但它是我可以为您提供的最接近的解决方案。希望这可以帮助。:)

于 2010-10-18T10:15:05.187 回答