2013-01-23 29 views
-4

我一直在研究一个小应用程序,并在创建新帖子的同时,希望我的应用程序检查数据库中是否存在特定文本值,特别是查找“已完成”。任何人都知道我可以做一个“如果”在铁轨?

到目前为止我想出了这个;

@job = current_user.Jobs.where(:all, :project_status => "completed") 
if @job.exists? 
    do something 
else 
    send an error 
end 

这是完全错误的,在阅读了一些内容并通过几个例子之后,我认为下面是做同样的事情;

@job = current_user.Jobs.exists?(:conditions = {:project_status => "completed"}) 

,但我没有找到我的研究一个例子,其中上述线路进入“if”条件,我只是做;

@job = current_user.Jobs.exists?(:conditions = {:project_status => "completed"}) 
    if @job 
     do something here 
    else 
     do something else here 
    end 

会更短,更清洁和正确吗?

+0

定义“不成功”; 'if'是'if'。什么是'拥有'? –

回答

3
if @job.project_status.include?("completed") 
#something here 
else 
#nothing 
end 
0

我不确定列中有什么内容。包含可能不如评估字符串那么快。

if @job.project_status == 'completed' 
#something here 
end 

这可能会比include更快吗?如果没有事情发生,你也不需要别的东西。

相关问题