2015-09-12 57 views
1

我的關係爲organizationsadvisorsorganization_advisorsorganization_advisors的字段爲is_verified。我想找到任何advisors爲具有is_verified爲false的特定organization。像這樣的事情(不工作)集合中的任何元素均爲

<% unless @organization.advisors.is_verified.any? %> 
    Approve Advisors 
<% end %> 

我也試過:

<% unless @organization.organization_advisors.is_verified.any? %> 
    Approve Advisors 
<% end %> 

我需要知道的任何@organization顧問都沒有is_verified。我將如何做到這一點?

回答

2

由於is_verifiedorganization_advisers就足夠了檢查其中是否存在:

<% unless @organization.organization_advisors.where(is_verified: false).any? %> 
    Approve Advisors 
<% end %> 

我假設你有以下結構:

class Organization 
    has_many :organization_advisors 
    has_many :advisors, through: :organization_advisors 
end 

class OrganizationAdvisor 
    belongs_to :organization 
    belongs_to :advisor 
end 

class Advisor 
    has_many :organization_advisors 
    has_many :organizations, through: :organization_advisors 
end 

在這裏也可以看到類似的問題:how to receive objects by condition in connecting table

+0

我採取了顛簸,並提出你的。你的是正確的,它的工作原理。謝謝。 –

+0

不客氣! –

+0

@CarySwoveland我的答案與顛簸不同,因爲他的代碼假設錯誤模型中的is_verified字段,所以我提出了正確的解決方案並闡明瞭數據庫結構。 –

2

試試這個:

<% unless @organization.advisors.where(is_verified: false).any? %> 
    Approve Advisors 
<% end %> 
+0

非常坎坷。我需要將'@ organization.advisors'更改爲'@ organization.organization_advisors',因爲它是帶有'is_verified'的表。萬分感謝。 –