How to include an additional join for use by a sql fragment attribute?

35 views
Skip to first unread message

Myron Marston

unread,
Oct 16, 2009, 4:22:33 PM10/16/09
to Thinking Sphinx
I've got a model that I'm trying to index like this:

class Listing < ActiveRecord::Base
has_many :listing_images

define_index do
# ...
has "COUNT(listing_images.id) > 0", :as => :has_images, :type
=> :boolean
# ...
end
end

Note that I'm not using the listing_images association anywhere else
in this index. When I run rebuild my index, I get this error:

ERROR: index 'listing_delta': sql_range_query: Unknown column
'listing_images.id' in 'field list'

This makes sense, since the listing_images table wasn't included as a
join in the generated SQL. I've gotten it to work by adding another
attribute that uses the listing_images association, like so:

has listing_images(:id), :as => :listing_image_id

However, I'm not happy with this solution since I really don't care to
index the listing_image id's. This attribute exists just to force TS
to include listing_images as a join in the generated SQL.

Is there a simpler, more elegant way to get TS to include
listing_images as a join, without having to define another attribute
for it?

Thanks!
Myron

James Healy

unread,
Oct 16, 2009, 6:25:06 PM10/16/09
to thinkin...@googlegroups.com
Myron Marston wrote:
> This makes sense, since the listing_images table wasn't included as a
> join in the generated SQL. I've gotten it to work by adding another
> attribute that uses the listing_images association, like so:
>
> has listing_images(:id), :as => :listing_image_id
>
> However, I'm not happy with this solution since I really don't care to
> index the listing_image id's. This attribute exists just to force TS
> to include listing_images as a join in the generated SQL.
>
> Is there a simpler, more elegant way to get TS to include
> listing_images as a join, without having to define another attribute
> for it?

I'm fairly sure the way you've worked around it is currently the best
eay to do so.

-- James Healy <ji...@deefa.com> Sat, 17 Oct 2009 09:25:01 +1100

Pat Allan

unread,
Oct 19, 2009, 11:51:06 AM10/19/09
to thinkin...@googlegroups.com
Agreed, there's no easier way at this point in time. Feel free to add
it as a feature request to the GitHub issues, though - it would be
nice to force a join via a method within define_index.

--
Pat

Myron Marston

unread,
Oct 19, 2009, 5:08:14 PM10/19/09
to Thinking Sphinx
Thanks, Pat. I've added the issue here:

http://github.com/freelancing-god/thinking-sphinx/issues/#issue/48
Reply all
Reply to author
Forward
0 new messages