3

我尝试了所有可以在网上找到的组合,但总是失败。如何从模型中检查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 
+0

你可以用”除非match.presen因为你没有使用特殊的Profile实例,在这里。不是Profile类有一个匹配,而是配置文件实例。 –

+0

@OlivierElMekki所以任何方式如何做到这一点然后使用lambda或许? – Rubytastic

+1

我自己并没有使用太阳黑子,但它似乎可以处理传递过程的条件索引[as:if option](http://mikepackdev.com/blog_posts/19-conditional-indexing-with-sunspot)。 –

回答

相关问题