Rails Code Review 2: 不要循环ActiveRecord

THE WRONG WAY:
@a = A.find_by_name("hideto")

@list = []
@a.bs.each do |b|
  b.cs.each do |c|
    @list << c if c.ccc?
  end
end

这样将产生许多条SQL语句

THE RIGHT WAY:
@a = A.find_by_name("hideto", {:include => {:bs => :cs}})

@list = []
@a.bs.each do |b|
  b.cs.each do |c|
    @list << c if c.ccc?
  end
end

这样只产生一条SQL语句就完成了所有的查询

你可能感兴趣的:(C++,c,C#,Rails,ActiveRecord)