How to define a WHERE condition on an association

53 views
Skip to first unread message

gmoniey

unread,
Nov 13, 2012, 6:11:42 PM11/13/12
to thinkin...@googlegroups.com
Hi,

I have a simple define index, and I want to limit the indexed records to those that have an association on a nullable field. Here is what my define_index block looks like:

  define_index do
    indexes :name, :sortable => true
    
    has :format_id, :created_at
    has assoc(:format).name, :as => :format_name
        
    where "my_table.deleted = 0 AND (format_id IS NULL OR format_name LIKE 'Indexable Format Name%')"
    
    set_property :delta => :delayed
  end

The error that I get when indexing is: Unknown column 'format_name'. I was wondering if the above is even possible?

Also, just wanted to double check that my assoc has line is correct? Seems like 'format' is a reserved keyword.

Thanks.

Pat Allan

unread,
Nov 14, 2012, 6:09:40 AM11/14/12
to thinkin...@googlegroups.com
Yeah, rake or ruby probably add a format method to the global scope. Wouldn't surprise me.

Try using the column reference as opposed to the alias in your WHERE clause: formats.name instead of format_name.

Cheers

--
Pat

> --
> 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/-/YCHISgI1onAJ.
> 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.



gmoniey

unread,
Nov 19, 2012, 12:39:54 AM11/19/12
to thinkin...@googlegroups.com
Thanks Pat...looks like that worked!
Reply all
Reply to author
Forward
0 new messages