开发者

Searchlogic and :has_many, :through =>

I'm using Searchlogic to search on many fields in a database. One of those fields is a :has_may, :through => relationship, and I can't get it to work.

Here are the relevant parts of the models:

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

Then, in m开发者_C百科y view, I have:

<% 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 %>

The search page loads just fine, but hitting the "submit" button gives me the following error:

 Searchlogic::Search::UnknownConditionError in SourcesController#index

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

Here is the code from SourcesController:

class SourcesController < ApplicationController

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

And here are the parameters:

Parameters: {"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"=>""}}

Does anyone have any ideas about what is happening here? Do you need to see more of the error message?

Many thanks!


I too have come across this limitation on searchlogic. there is a 'hack' of a solution that i got. its simple really. search for items you looking for on the join model. this will give you the ids of the models that have that search. this is a bit quirky but its the closest solution i could offer you. hope this helps. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜