我(想要)在父模型(groups
)中有一个方法来检查孩子(subjects
)是否有孩子(goals
)
groups.rb
:
def has_goals?
@answer = []
subjects = self.subjects
subjects.each do |subject|
if subject.try(:goals).present?
@answer << true
else
@answer << false
end
end
if @answer.include?("true")
true
else
false
end
end
我会这样使用它-
if group.has_goals?
# do something
else
# do something else
end
目前它不起作用,因为它正在返回false
所有东西 - 无论是否subject
有goals
。任何想法如何让这个工作?