Как проверить, существует ли ассоциация в ActiveRecord из модели?
Я перепробовал все комбинации, которые мог найти в Интернете, и это всегда не удавалось.
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
а также
Match belongs_to :profile
Внутри модели профиля я пытаюсь сделать:
unless profile.match.exist? (does profile have a match association existing?)
.. do something
end
1 ответ
Решение
Вдохновленный постом в блоге, который Оливье связал и подтвердил в документации Sunspot, вы можете сделать следующее:
# In your Profile model
searchable :if => proc { |profile| profile.match.present? } do
integer :id
string :country
string :state
end