Ruby(或Rails)是否提供了一种简单的方法来完成以下任务:
if @author.articles.each(:published == true)
puts "all articles of this author are published"
end
我认为这个例子不言而喻。
答案 0 :(得分:0)
也许有人会找到更好的东西,但这里有一些可行的方法:
unless @author.articles.map{|a| a.published == true}.include?(false)
puts "all articles of this author are published"
end
...或
if @author.articles.select{|a| !a.published}.size == 0
puts "all articles of this author are published"
end
...或
if @author.articles.detect{|a| !a.published}.nil?
puts "all articles of this author are published"
end
我倾向于最后一个。
答案 1 :(得分:0)
或者,如果您不必加载文章......
if @author.articles.count == @author.articles.count(:conditions => { :published => true })
puts "..."
end
答案 2 :(得分:0)
我认为还有另一个:
如果@ author.articles.all?(&:published?)
将“所有#{@author.name}的文章发布”