Intercepting creation of new object.
Posted
by amctammany
on Stack Overflow
See other posts from Stack Overflow
or by amctammany
Published on 2010-05-22T02:53:31Z
Indexed on
2010/05/22
3:00 UTC
Read the original article
Hit count: 475
ruby-on-rails
|validation
I'm adding a categorization functionality to my app and struggling with it. Objects have many categories through categorizations. I'm trying to intercept the creation of a new categorization, check if theres a similar one, if so, increment it's count, if not, create a new object. Here's what I have so far.
validate :check_unique
protected
def check_unique
categorization = Categorization.where(:category_id => self.category_id, :categorizable_id => self.categorizable_id, :categorizable_type => self.categorizable_type)
if categorization.first
categorization.first.increment(:count)
end
end
© Stack Overflow or respective owner