Checking if ActiveRecord find returns a result

后端 未结 7 1510
孤独总比滥情好
孤独总比滥情好 2021-02-07 08:03

I\'m trying to check if a find method returns a result. My find method is the following:

post = Post.find(:all, :conditions => { :url => params[\'url\'] },         


        
相关标签:
7条回答
  • 2021-02-07 08:33

    it may be as simple as changing your finder to:

    post = Post.find(:first, :conditions => { :url => params['url'] })
    

    With this finder, post will either return a single value or nil. Because nil behaves like false in a condition statement, you can say something like the following:

    if post
      # do something
    else
      # do something else
    end
    
    0 讨论(0)
提交回复
热议问题