Proper way to use before_create
- by ruevaughn
Pretty basic question here, I need to write a before filter on my Category model, to ensure that the depth never reaches more than 2. Here is what I have so far.
app/models/category.rb
before_create :check_depth
def check_depth
self.depth = 1 if depth > 2
end
I need it instead of setting depth to 1, just to return a error message, but I can't even get this current setup to work, I get the error
undefined method `>' for nil:NilClass
So, instead of setting the depth to one like I'm trying to do how would I send an error instead? And any help getting the current function working for informational purposes? Thanks in advance