Joey
Joey

Reputation: 762

Rails Return Error on Class Method

I have a class method that I would like to return a error from, is something like this possible?

class Foo < ActiveRecord::Base
 def self.do_this
  if b = Bar.find_by_id(5)
   return 'Yea' 
  else
   self.errors.add_to_base('I was not found')
  end
 end
end

Upvotes: 0

Views: 897

Answers (1)

Hoa
Hoa

Reputation: 3207

No, it's not possible because errors is an instance method while in this case self is the Foo class.

If you run this code, you'll get an error as follows:

undefined method `errors' for #<Class:0xb5f376d0>

Upvotes: 1

Related Questions