4

I tried all combinations that I could find online and it always fails.

class Profile < ActiveRecord::Base

  belongs_to  :user, :dependent => :destroy
  has_one     :match # educational matches 

  accepts_nested_attributes_for :match
  attr_accessor                 :form

  unless match.present?
    searchable do

      integer :id
      string :country
      string :state
    end
  end
end

and

Match belongs_to :profile

Inside the Profile model I try to do:

  unless profile.match.exist? (does profile have a match association existing?) 
    .. do something

  end
carols10cents
  • 6,943
  • 7
  • 39
  • 56
Rubytastic
  • 15,001
  • 18
  • 87
  • 175
  • You can't do `unless match.present?` at the class level, since you're using no special Profile instance, here. It's not the Profile class that has one match, it's profile instances. – kik Oct 19 '13 at 16:19
  • @OlivierElMekki so any way how to do it then using a lambda perhaps? – Rubytastic Oct 20 '13 at 21:04
  • 1
    I don't use sunspot myself, but it seems it can handle conditional indexing passing proc [as :if option](http://mikepackdev.com/blog_posts/19-conditional-indexing-with-sunspot). – kik Oct 21 '13 at 00:35

1 Answers1

2

Inspired by the blog post that Olivier linked to and confirmed in the Sunspot documentation, you could do:

# In your Profile model
searchable :if => proc { |profile| profile.match.present? } do
  integer :id
  string :country
  string :state
end
carols10cents
  • 6,943
  • 7
  • 39
  • 56