Rails def for testing relations count -


i have model location. contains:

has_many :woassets has_many :workorders has_many :contacts has_many :worequests has_many :costprojects has_many :storerooms has_many :employees has_many :jobplans has_many :woschedules 

in view, don't want delete button if location has records associated. so, i'm doing this:

<% if location.workorders.count == 0 , location.woassets.count == 0 , location.contacts.count == 0 ...   %> 

is there better way put def in model , use in view?

thanks help!

class location    def has_assoc?     [:rel1, :rel2, ...].any? { |r| send(r).present? }   end end 

or @ associations, rather specific list:

def has_assoc?   location.reflections.keys.any? { |r| send(r).present? } end 

in view:

<% if location.has_assoc? %> 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -