Jens Kraemer wrote:
> On Tue, May 15, 2007 at 05:37:19PM +0200, Jacob Robbins wrote:
>> rails convention of creating a new active record object to store user
>> query params. I'd like to make a regular rails form using a blank object
>> and then call more_like_this on that object to do a search.
>
> This isn't supported by aaf but should be possible to do with a bit of
> hacking :)
>
> It'll get a bit harder if you want to do this with the DRb server, since
> then you'll have to transfer your unsaved record over to the server for
> the more_like_this query to be built. Atm only id and class name
> are transferred with method calls.
>
> Jens
Thanks for checking into this Jens, i've done what i wanted by adding an
instance method to aaf. In instance_methods.rb, right after the to_doc
method, i added a to_ferret_query method. This avoids transfering the
whole object when using the DRb server. Tell me what you think...
>>>>>>>>>>>>>>>>>>>
# Turn this instance into a ferret query derived from its field
values.
# Empty fields are ignored. Can be used on unsaved records. Typical
use is to make
# ferret query from a new object initialized from posted form
values.
#
# Example: college.to_query(:fuzz => 0.6)
# #=> "name:seattle~0.6 and name:university~0.6 and
city:seattle~0.6"
#
#
# === Options
#
# fuzz:: Default: nil. Float value for fuzziness to attach
to search terms.
# field_names:: Default: nil. (uses ferret indexed fields) Array
of field names to use in query.
# join_type:: Default: 'and'. String used to join query terms.
# exclude:: Default: ['and', 'or']. Array of words to ignore
in field values.
def to_ferret_query(options = {})
options = {
:field_names =>
self.class.aaf_configuration[:ferret_fields].keys,
:join_type => 'and',
:exclude => ['and','or']
}.update(options)
terms = []
options[:field_names].each do |field|
if val = self.send(field)
val.to_s.split.each do |word|
unless options[:exclude].include?(word.strip.downcase)
terms << field.to_s + ':' + word + ( options[:fuzz] ? '~'
+ options[:fuzz].to_s : '' )
end
end
end
end
terms.join ' ' + options[:join_type] + ' '
end
<<<<<<<<<<<<<<<<<<<<<<<<<<
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk