Search code examples
ruby-on-railsrubyruby-on-rails-3ruby-on-rails-3.1rails-activerecord

Error on get record in Rails Helper


When o try verify in my helper if my database have the record this stop on the verifier whit this error:

ActiveRecord::RecordNotFound in FinancesController#index

Couldn't find Place with id=5

Im using this helper:

  def get_acc_name(acc_id)
    @var = Place.find(acc_id)
    if @var.kind_of?(Array)
    @var = @var.place 
    else
    @var = "(Deletado)"
    end
    return @var
  end  

The problem is the script stop on the first line and font make a if else, i dont know how i solve this please


Solution

  • You can use where instead of find. find raises exception if a record is not found. So, use this instead:

    @var = Place.where(:id => acc_id).first
    

    or

    @var = Place.find_by_id(acc_id)
    

    They will not raise exception. :)