我想创建像这样的查询:如何创建一个在存在一条或多条记录时设置为真的查询?
@step1_completed = IF 1 or more records exists return true, else false
我想创建像这样的查询:如何创建一个在存在一条或多条记录时设置为真的查询?
@step1_completed = IF 1 or more records exists return true, else false
假设record
是一个模型,
@step1_completed = Record.any?
我确定喜欢这个想法,但它错误:未定义的方法'任何? – AnApprentice 2011-05-08 23:20:56
如何如下:
@step1_completed = Record.first ? true : false
Just Record.first在这种情况下会自行返回真值。如果你想将它转换为'true'或'false',只要'!! Record.first'。你不需要'? true:false' – 2011-05-09 00:42:20
的exist?
方法对本作:
Record.exist?
它将执行查询以检查表格是否存在单个记录,如果存在,将返回true
。
什么是“记录”?它是模型的一个实例吗? – thefugal 2011-05-08 23:13:21
不是答案,但为什么不做'@ step1_completed = 1_or_more_records_exists?'? – sawa 2011-05-08 23:15:06