Proper way to use before_create
Posted
by
ruevaughn
on Stack Overflow
See other posts from Stack Overflow
or by ruevaughn
Published on 2012-04-15T05:24:28Z
Indexed on
2012/04/15
5:29 UTC
Read the original article
Hit count: 92
ruby-on-rails
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
© Stack Overflow or respective owner