在辅助方法中返回数组

时间:2013-02-08 00:44:42

标签: ruby-on-rails arrays helpermethods

为什么我不能在辅助方法中返回数组?

def childrenOf(a)
    @children = Post.find_by_parent_id(a.id)        
    return @children 
end

提前致谢

2 个答案:

答案 0 :(得分:2)

你可以。

改为使用find_all_by_parent_id

你不需要第二行。

以下就足够了。

def childrenOf(a)
  @children  = Post.find_all_by_parent_id(a.id)        
end

答案 1 :(得分:0)

在Rails 3中,而不是使用find_all_by使用where

def childrenOf(a)
  @children  = Post.where(:parent_id => a.id)        
end