これらの関連付けでは、拡大結果のみが正しい結果を返しますが、2番目の関連付けを検索しようとすると、0の結果が返されます。
has_one :magnification,
:class_name => 'ProductAttribute',
:foreign_key => 'product_id',
:conditions => {:key => 'Magnification'}
has_one :objective_lens,
:class_name => 'ProductAttribute',
:foreign_key => 'product_id',
:conditions => {:key => 'Objective Lens Diameter'}
define_index do
has magnification(:value), :type => :float, :as => :magnification
has objective_lens(:value), :type => :float, :as => :objective_lens_diameter
end
使用したサンプルコード
# returns expected results
Product.search(nil, :with => {:magnification => (8.0..9.0)})
# returns 0 results
Product.search(nil, :with => {:objective_lens_diameter => (31.0..61.0)})
しかし、define_indexの順序を逆にすると、逆のことが起こります。したがって、対物レンズの直径の結果は正しい結果を返し、倍率の結果は0を返します。
Rails v2.2、Thinking-Sphinxをプラグインv1.2.12およびSphinx0.9.8として使用
編集:生成されたsql_query値を見ると、2番目の属性の結合が間違った関連付けを使用しているため、期待される結果が返されません。
簡略化された結果:
SELECT
`products`.`id` * 2 + 1 AS `id`,
`products`.`id` AS `sphinx_internal_id`,
1234567890 AS `class_crc`,
`product_attributes`.`value` AS `magnification`,
`objective_lens_products`.`value` AS `objective_lens_diameter`
FROM `products`
LEFT OUTER JOIN `product_attributes` ON product_attributes.product_id = products.id
AND `product_attributes`.`key` = 'Magnification'
LEFT OUTER JOIN `product_attributes` objective_lens_products ON objective_lens_products.product_id = products.id
AND `product_attributes`.`key` = 'Objective Lens Diameter'
WHERE `products`.`id` >= $start
AND `products`.`id` <= $end
GROUP BY `products`.`id`
ORDER BY NULL