Re: [ts] boolean attribute from has_may association

247 views
Skip to first unread message

Pat Allan

unread,
Jul 14, 2012, 7:18:13 AM7/14/12
to thinkin...@googlegroups.com
Hi Gordon

Ah, that's a little tricky. There's three approaches I can think of.

Option A: add another association to A for just active Bs:
has_many :active_bs, :class_name => 'B', :conditions => {:active => true}

And then add a join to that association in your index, plus the attribute using a bit of SQL. If you're using your original bs association somewhere, you'll need to make sure you get the table alias correct in this SQL snippet:

define_index do
join active_bs
has "COUNT(bs.id) > 0", :as => :active_bs, :type => :boolean
end

Option B (MySQL only!): Use the standard association, but put more complexity into the SQL snippet:

define_index do
join bs
has "SUM(bs.active) > 0", :as => :active_bs, :type => boolean
end

Option C is just like B, but for PostgreSQL. This is where it gets complex, as booleans are data types here:

define_index do
join bs
has "SUM(CASE bs.active WHEN TRUE THEN 1 ELSE 0 END) > 0", :as => :active_bs, :type => :boolean
end

Hope one of these suits.

Cheers

--
Pat

On 13/07/2012, at 2:56 PM, Gordon Friebe wrote:

> Hi,
>
> I have a Model A that is being indexed. Connected to model A via a has_may relatiosnhip is model B.
> Model B has a boolean attribute active.
>
> My index needs an attribute that shows me whether there are any active Bs connected to A.
>
> define index do
> has bs, as: :active
> end
>
> So I need to do A.search with: {active: true}.
>
> How can this be done? I just cannot find how to do this with thinking sphinx.
> Thanks a lot!
>
> Gordon
>
> --
> You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/thinking-sphinx/-/d4Kp9g2wAWoJ.
> To post to this group, send email to thinkin...@googlegroups.com.
> To unsubscribe from this group, send email to thinking-sphi...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/thinking-sphinx?hl=en.

Gordon Friebe

unread,
Jul 17, 2012, 4:47:36 AM7/17/12
to thinkin...@googlegroups.com
Hi Pat,

I only now had the opportunity to test it.
Solution 1 diodnt work for me, I got an unknown assoziation active_bs error.
Solution 2 works!! 
What I was missing was the SQL part in the has attribute ("SUM(bs.active) > 0"). I didnt see the abillity to to that. It also works without the join statement.

Thanks a lot for pointing me to ths solution. It saved me a lot of time.

Cheers,
Gordon
> To post to this group, send email to thinking-sphinx@googlegroups.com.
> To unsubscribe from this group, send email to thinking-sphinx+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages