Hi Allen!
comments inline
On Fri, Aug 10, 2007 at 05:21:38AM +0200, Allen Young wrote:
> Suppose we have a model "Product", in "Product" I've declared a
> polymorphic relationship with model "Property1" and "Property2", the
> following code will show this:
>
> class Product < ActiveRecord::Base
> belongs_to :property, :polymorphic => true
> @@ferret_fields = {...}
> acts_as_ferret({:fields => @@ferret_fields})
> end
>
[..]
>
> Now I want to provide full text search capability for "Product" and it's
> obvious that "Product" should contains its "property" while being
> indexed. So I should define "ferret_fields" class method in "Property1"
> and "Property2" to collect all their fields and dynamically define the
> corresponding method in "Product". The code is something like this:
>
[..]
>
> class Product < ActiveRecord::Base
> belongs_to :property, :polymorphic => true
> @@ferret_fields = {...}
> @@ferret_fields.merge!(Property1.ferret_fields)
> @@ferret_fields.merge!(Property2.ferret_fields)
> acts_as_ferret({:fields => @@ferret_fields})
> Property1.ferret_fields.keys.each do |field|
> define_method("#{field}") do
> result = property.send("#{field}")
> end
> end
> Property2.ferret_fields.keys.each do |field|
> define_method("#{field}") do
> result = property.send("#{field}")
> end
> end
> end
>
> But there are two problems in the above code:
>
> 1. If the property object in a product object is "Property1",
> property.send("#{field}") in "Property2"'s block will cause a method
> missing error, vice versa.
I'd just rescue that and return nil for the field:
Property2.ferret_fields.keys.each do |field|
define_method("#{field}") do
result = property.send("#{field}") rescue nil
end
end
> 2. Say "Property1" has 500 fields as well as "Property2", each product
> will be indexed using 1000 fields while only at most 500 fields contains
> value.
Do you really need to be able to run queries against each single one of
these 1000 fields? If not, you could concatenate their values into a
single large :properties field.
Cheers,
Jens
--
Jens Krämer
http://www.jkraemer.net/ - Blog
http://www.omdb.org/ - The new free film database
_______________________________________________
Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk