count records of associated model

64 views
Skip to first unread message

rtacconi

unread,
Jan 3, 2012, 1:52:26 PM1/3/12
to Thinking Sphinx
Hi,

I have a project model which has a filter:

has paid, :type => :boolean

If paid checkbox is flagged, sphinx searches for paying projects. the
problem is that some projects have not any course:

project.courses.count # returns 0

I there a way to search for paying projects having at least one
course?

paid => true and projects.courses.count > 0

at the moment I have paid sent in the conditions

A solution could be to add a field set to true if its courses are more
than zero, but it is a redundant information

Pat Allan

unread,
Jan 3, 2012, 7:07:37 PM1/3/12
to thinkin...@googlegroups.com

Sphinx has no concept of the number of values (no COUNT() function), so you'll need to create an attribute for that purpose:

has "COUNT(DISTINCT courses.id) > 0", :as => :has_courses, :type => :boolean
join courses # if you're not referring to the association elsewhere

And then your search would be:

Project.search :with => {:paid => true, :has_courses => true}

Cheers

--
Pat

> --
> You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.
> 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.
>

rtacconi

unread,
Jan 13, 2012, 4:33:49 AM1/13/12
to Thinking Sphinx
Thank you :-)

rtacconi

unread,
Jan 13, 2012, 5:32:05 AM1/13/12
to Thinking Sphinx
In my mac you example works well. In the Ubuntu server when I rebuild
the indexes I get a seg fault:

Stopped search daemon (pid 19884).
Generating Configuration to /home/wpn/production/releases/
20120113102215/config/production.sphinx.conf
Sphinx 0.9.8.1-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff

using config file '/home/wpn/production/releases/20120113102215/config/
production.sphinx.conf'...
indexing index 'project_core'...
collected 708 docs, 1.8 MB
sorted 0.3 Mhits, 100.0% done
Segmentation fault
Started successfully (pid 19907).

And sphinx does not start properly after that. If I comment 'join
courses' the seg fault error goes any. Any idea why this is happening?

The version in the mac is Sphinx 0.9.9-release. In Ubuntu is Sphinx
0.9.8.1-release

On Jan 4, 1:07 am, "Pat Allan" <p...@freelancing-gods.com> wrote:

rtacconi

unread,
Jan 13, 2012, 5:48:53 AM1/13/12
to Thinking Sphinx
this is my index in Project model:

define_index do
indexes name
indexes summary
indexes description
indexes phone
indexes email
indexes website
indexes climate_zone
indexes city
indexes address
indexes state
indexes post_code
indexes country
indexes project_type

has created_at
has show, :type => :boolean
has paid, :type => :boolean
has lat, lng
has "COUNT(DISTINCT courses.id) > 0", :as => :has_courses, :type
=> :boolean
# join courses

indexes posts.created_at, :as => :posts, :sortable => true
set_property :delta => true
end

And I have this inside the Project model too:

has_many :courses

With 'join courses' commented I do not get the segmentation fault
error
On Jan 4, 1:07 am, "Pat Allan" <p...@freelancing-gods.com> wrote:

rtacconi

unread,
Jan 18, 2012, 5:11:46 AM1/18/12
to Thinking Sphinx
Any idea?

Timo Virkkala

unread,
Jan 18, 2012, 5:41:23 AM1/18/12
to thinkin...@googlegroups.com
Hi,

Would something like this work?

has '(select count(courses.id) from courses where
courses.project_id=projects.id)', :as => :courses_count, :type =>
:integer

I've done something similar in my own project.

-Timo

Reply all
Reply to author
Forward
0 new messages