[from out-of-band communication with steven, just for the record] Hi! On Wed, Apr 11, 2007 at 01:47:49PM +0200, Steven Garcia wrote:> Hey Jens, >[..]> > 1. Is the Search model just an Active Record object or do I need to > set up a database for it as well (if so then with what columns)?No, that search model is just a plain class that backs your search form. If you want to take advantage of AR''s validations for the search form, you can use the active_record_base_without_table rails plugin that gives you a base class for your model similar to ActiveRecord::Base, but without the need for a db table :-)> 2. Do I need to set up table relationships to the other models > (has_many) or do my models just inherit from the search class?No and no :-) A sample search model might look like this: class Search attr_accessor :query def do_search return Model.find_by_contents(query) end end any query preprocessing or extended search form fields would go in there, too. In your controller''s search action you then only need to do search = Search.new(params[:search]) @results = search.do_search> 3. Can I still boost certain fields within various models?As the boost for fields is set in your call to acts_as_ferret in the specific model you want to index, there''s no problem setting this in various models. Btw, if you want to search across multiple Model classes at once, use the multi_search method instead of find_by_contents. Jens -- Jens Kr?mer webit! Gesellschaft f?r neue Medien mbH Schnorrstra?e 76 | 01069 Dresden Telefon +49 351 46766-0 | Telefax +49 351 46766-66 kraemer at webit.de | www.webit.de Amtsgericht Dresden | HRB 15422 GF Sven Haubold, Hagen Malessa
Thanks for the response Jens, However, I think I lack basic understanding of how multi_search operates. You can see what I am working with here: http://pastie.caboo.se/53825 RIght now I get this error: ------------------------------------------------------------------ ArgumentError in SearchesController#search wrong number of arguments (1 for 0) ------------------------------------------------------------------ I am sure that I am doing something fundamentally stupid. Please help!
On Sat, Apr 14, 2007 at 05:50:08PM +0200, Steven Garcia wrote:> Thanks for the response Jens, > > However, I think I lack basic understanding of how multi_search > operates. > > You can see what I am working with here: > > http://pastie.caboo.se/53825your multi_search call is broken, it should read: def self.do_search return multi_search(query, [Article, Term]) end Does that help? Jens -- Jens Kr?mer webit! Gesellschaft f?r neue Medien mbH Schnorrstra?e 76 | 01069 Dresden Telefon +49 351 46766-0 | Telefax +49 351 46766-66 kraemer at webit.de | www.webit.de Amtsgericht Dresden | HRB 15422 GF Sven Haubold, Hagen Malessa
I am returning nil values for my results, and the cause is obvious.. I havent set my @results instance variable anywhere. Thing is, I''m not sure where or how to set it, since my results can be both terms and articles. Do I need to have search actions in each of my controllers? If so do I set @results there, or is there some catch-all method I can employ in SearchesController? I have updated my pastie to show theview code I have written for my results, if that helps. http://pastie.caboo.se/54178
On Mon, Apr 16, 2007 at 03:19:29PM +0200, Steven Garcia wrote:> I am returning nil values for my results, and the cause is obvious.. > I havent set my @results instance variable anywhere. Thing is, > I''m not sure where or how to set it, since my results can be both > terms and articles. > > Do I need to have search actions in each of my controllers? > If so do I set @results there, or is there some catch-all method > I can employ in SearchesController?this really depends on how your app is structured. If you have different kinds of searches, having different search actions in various controllers is perfectly fine. If you only have one search, but that may return instances of different classes (as is true with multi_search), you have to handle that in your view (or in a suitable helper method you use to display your results). Jens -- Jens Kr?mer webit! Gesellschaft f?r neue Medien mbH Schnorrstra?e 76 | 01069 Dresden Telefon +49 351 46766-0 | Telefax +49 351 46766-66 kraemer at webit.de | www.webit.de Amtsgericht Dresden | HRB 15422 GF Sven Haubold, Hagen Malessa
For clarification purposes, I am trying to return instances from multiple models, meaning just one multi_search method for all. I am convinced that I am not writing my Search Controller correctly. I am trying to use full text search and pagination and have no idea how to 1. hook into these various models in my controller action 2. call up the results in my view Here is the updated code: http://pastie.caboo.se/54465
On Tue, Apr 17, 2007 at 08:26:04PM +0200, Steven Garcia wrote:> For clarification purposes, I am trying to return instances from > multiple models, meaning just one multi_search method for all. I am > convinced that I am not writing my Search Controller correctly. I am > trying to use full text search and pagination and have no idea how to > > 1. hook into these various models in my controller action > 2. call up the results in my viewFirst of all, the full_text_search methods in your model classes won''t help you a thing since you aren''t calling them. What you call is your multi_search call in the search model. So that''s where you have to implement the paging. However if I were you I''d first try to get this working without pagination. Once this works, all that''s left is to implement pagination across that set of results. To deal with having different models in your @results array, build a helper method to handle this: module SearchHelper # might be extended to not only render the name of a record, but a # link to a details page, too. Always depending of the record''s type, # of course. def search_result_name(result) case result when Term result.name when Article result.title else result.to_s end end end Now use that in your view: <ul> <% @results.each do |result| %> <li><%= search_result_name result %></li> <% end %> </ul> For the pagination stuff you''ll have to implement the page selection that is done in the unused full_text_search methods in your search model, should be easy to adapt since multi_search takes the same options as find_by_contents. Jens -- Jens Kr?mer webit! Gesellschaft f?r neue Medien mbH Schnorrstra?e 76 | 01069 Dresden Telefon +49 351 46766-0 | Telefax +49 351 46766-66 kraemer at webit.de | www.webit.de Amtsgericht Dresden | HRB 15422 GF Sven Haubold, Hagen Malessa
Thanks for this helper code, Jens. much appreciated. :-) I just wish I could make it to the view in the first place. My search controller method is calling up some strange errors. Take a look when you can.. http://pastie.caboo.se/54629
On Wed, Apr 18, 2007 at 02:20:26PM +0200, Steven Garcia wrote:> Thanks for this helper code, Jens. much appreciated. :-) > > I just wish I could make it to the view in the first place. My search > controller method is calling up some strange errors.your Search model doesn''t define a 1-argument constructor (it defines no constructor and therefore only has a 0-argument one). try adding def initialize(query) @query = query end to your search model. Jens -- Jens Kr?mer webit! Gesellschaft f?r neue Medien mbH Schnorrstra?e 76 | 01069 Dresden Telefon +49 351 46766-0 | Telefax +49 351 46766-66 kraemer at webit.de | www.webit.de Amtsgericht Dresden | HRB 15422 GF Sven Haubold, Hagen Malessa
Hmm, I added your method to my model but am still getting the same errors in my controller action With this code, I get undefined method `do_search def search search = Search.new(params[:query]) @results = search.do_search end Which makes no sense at all because in my model I have defined the method def self.do_search return multi_search(query, [Article, Term]) end This is beyond weird. Even if my do_search method is not well- written, it should not come up as undefined. Just for laughs i got rid of the "self." part of that method to see what happens. Whaddya know? undefined method `multi_search'' This is even stranger. I know that my AAF is loaded correctly because I can do single-model searches no problem. Have I inadvertently stumbled on a bug? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/ferret-talk/attachments/20070418/4d6d38a3/attachment-0001.html
On Wed, Apr 18, 2007 at 05:05:49PM +0200, Steven Garcia wrote:> Hmm, I added your method to my model but am still getting the same > errors in my controller action > > With this code, I get undefined method `do_search > > def search > search = Search.new(params[:query]) > @results = search.do_search > end > > Which makes no sense at all because in my model I have defined the > method > > def self.do_search > return multi_search(query, [Article, Term]) > end > > This is beyond weird. Even if my do_search method is not well- > written, it should not come up as undefined. Just for laughs i got > rid of the "self." part of that method to see what happens. Whaddya > know? > > undefined method `multi_search''so you''re one step further now :-) using ''def self.search'' you declared a class method, but we wanted an instance method, which you declare by omitting the ''self.'' .> This is even stranger. I know that my AAF is loaded correctly because > I can do single-model searches no problem. Have I inadvertently > stumbled on a bug?No, you''re just missing the name of the class you call multi_search on (multi_search is a class method that gets created by aaf in the model class the acts_as_ferret call is in). Article.multi_search ... should finally work ;-) btw, Term.multi_search will, too, it doesn''t matter which of the classes you pick. Jens -- Jens Kr?mer webit! Gesellschaft f?r neue Medien mbH Schnorrstra?e 76 | 01069 Dresden Telefon +49 351 46766-0 | Telefax +49 351 46766-66 kraemer at webit.de | www.webit.de Amtsgericht Dresden | HRB 15422 GF Sven Haubold, Hagen Malessa
Alright, I am getting new errors now which means I''m making progress right? You have a nil object when you didn''t expect it! The error occurred while evaluating nil.constantize If this is not obvious enough I can post up a full trace on pastie. Let me know For the record, my search model now looks like this: class Search attr_accessor :query def initialize(query) @query = query end def do_search return Article.multi_search(query, [Article, Term]) end end And the only remotely ferret related code in Terms and Articles is acts_as_ferret :fields => { :title => { :boost => 2 }, :body => {:index => :untokenized} } -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/ferret-talk/attachments/20070418/d4ebb0d4/attachment.html
On Wed, Apr 18, 2007 at 06:56:19PM +0200, Steven Garcia wrote:> Alright, I am getting new errors now which means I''m making progress > right? > > You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.constantize > > If this is not obvious enough I can post up a full trace on pastie. > Let me knowYes, a trace would be useful so we know where this happens.> > For the record, my search model now looks like this: > > class Search > attr_accessor :query > > def initialize(query) > @query = query > end > > def do_search > return Article.multi_search(query, [Article, Term]) > end > end > > And the only remotely ferret related code in Terms and Articles is > > acts_as_ferret :fields => { :title => { :boost => 2 }, :body => > {:index => :untokenized} }looks ok. Jens -- Jens Kr?mer webit! Gesellschaft f?r neue Medien mbH Schnorrstra?e 76 | 01069 Dresden Telefon +49 351 46766-0 | Telefax +49 351 46766-66 kraemer at webit.de | www.webit.de Amtsgericht Dresden | HRB 15422 GF Sven Haubold, Hagen Malessa
Here is the full trace: http://pastie.caboo.se/54851
On Thu, Apr 19, 2007 at 02:09:02AM +0200, Steven Garcia wrote:> Here is the full trace: > > http://pastie.caboo.se/54851ok, you need to set :store_class_name => true in your model classes when you call the acts_as_ferret method. Jens -- Jens Kr?mer webit! Gesellschaft f?r neue Medien mbH Schnorrstra?e 76 | 01069 Dresden Telefon +49 351 46766-0 | Telefax +49 351 46766-66 kraemer at webit.de | www.webit.de Amtsgericht Dresden | HRB 15422 GF Sven Haubold, Hagen Malessa
Same error as before. It must be because I haven''t written out the config correctly. This is what I have in each of my models: acts_as_ferret :store_class_name => true, :fields => { :title => { :boost => 2 }, :body => {:index => :untokenized} } Here is the app trace just in case: http://pastie.caboo.se/55021
Okay, so I cleared out and regenerated my index and that seems to have gotten rid of the constantize error. Unfortunately that has been replaced by a new error (nil.latest) http://pastie.caboo.se/55186 Bizarre!
I still have not resolved this multi_search thing. I get a nil error for the constantize method. I have posted the complete code and full stack trace here: http://pastie.caboo.se/57957 Any help would be hugely appreciated
On Tue, May 01, 2007 at 05:27:55PM +0200, Steven Garcia wrote:> I still have not resolved this multi_search thing. I get a nil error > for the constantize method. I have posted the complete code and full > stack trace here: > > http://pastie.caboo.se/57957I''m pretty sure this is because you have an index where the class names are missing. In one of your last mails you mentioned that you solved this by rebuilding? Regarding your other problem (nil.latest? in multi_search), I changed something in the error handling in svn trunk so the problem should now be more obvious when this happens. Jens -- Jens Kr?mer webit! Gesellschaft f?r neue Medien mbH Schnorrstra?e 76 | 01069 Dresden Telefon +49 351 46766-0 | Telefax +49 351 46766-66 kraemer at webit.de | www.webit.de Amtsgericht Dresden | HRB 15422 GF Sven Haubold, Hagen Malessa