To edit pages or tickets please login with username/password: aaf/aaf

Ticket #188 (closed enhancement: fixed)

Opened 10 months ago

Last modified 8 months ago

[PATCH] Allow associating ferret fields with arbitrarily named model fields. WAS: Allow field names to be aliased.

Reported by: aaf Assigned to: jk
Priority: major Milestone:
Component: 0plugin Version:
Keywords: alias field patch Cc:

Description

This patch allows you to alias your model methods as some other desired field name in the ferret index. For instance, if you had a model method called 'media_type' but want the field in the ferret index to be just 'type', you can do it as follows:

class Media < ActiveRecord::Base
  acts_as_ferret(:fields => {:name => {},
                           :media_type => {:alias => :type},
                           :price_in_dollars => {:alias => :cost, :index => :untokenized}},
               :ferret => {:default_field => [:name, :type]})
end

Now when you search, you can use a query as follows:

name:tool type:cd cost:9

(To find all $9 cds with 'tool' in the name.)


Note that when the default search fields are specified, you refer to the alias. This is because, to ferret, the field is named for the alias, not the model method where the data comes from. As far as ferret knows, that is the real field.


I added the ability to alias for two reasons. First, because it frees me to name my methods descriptively and still have the field names in the query be easy and short. Obviously in a search for media, type is going to refer to a media type... the user shouldn't have to use 'media_type:cd' when 'type:cd' will suffice.

Second, and more importantly, certain methods are reserved by rails/ruby (like 'type') so you can't (easily) get away with using them for method names.

Xichekolas

Attachments

FieldAliasPatch.diff (1.4 kB) - added by aaf on 12/09/07 13:31:20.
aaf_patch_188.diff (4.4 kB) - added by aaf on 01/12/08 23:21:34.

Change History

12/09/07 13:31:20 changed by aaf

  • attachment FieldAliasPatch.diff added.

01/12/08 23:21:34 changed by aaf

  • attachment aaf_patch_188.diff added.

01/12/08 23:28:53 changed by aaf

  • priority changed from minor to major.
  • summary changed from [PATCH] Allow field names to be aliased. to [PATCH] Allow associating ferret fields with arbitrarily named model fields. WAS: Allow field names to be aliased..

Let me throw in my 2 cents. 1. To keep things straighter, I would do it in a bit reversed manner:

class Media < ActiveRecord::Base
  acts_as_ferret(:fields => {:name => {},
                           :type => {:via => :media_type},
                           :price_in_dollars => {:alias => :cost, :index => :untokenized}},
               :ferret => {:default_field => [:name, :type]})
end

here, the difference is that the key keeps it's semantics as the Ferret's name while the actual ActiveRecord? attribute name is specified by :via option.

2. The previous patch is incomplete and ain't gonna really work.

Attached: aaf_patch_188.diff, please review.

//ivg

02/02/08 12:52:38 changed by jk

  • status changed from new to closed.
  • resolution set to fixed.

Applied ivg's patch. Thanks!

To edit pages or tickets please login with username/password: aaf/aaf