repeat_purchases_percent is a method in my Category model. All parentless categories are guarenteed to have a value for future_purchases_percent in the database. Subcategories may or may not have a future_purchases_percent. if they do, i want to use that value. If not, I want to use the parent’s value and so on.
I tried a recursive method.
def future_purchases_percent
if self.future_purchases_percent.nil?
Category.find(self.parent_id).future_purchases_percent
else
future_purchases_percent
end
end
This gets stuck in a loop where it keeps evaluating:
if self.future_purchases_percent.nil?
how can i correctly implement this method?
a || bexpression.belongs_to :parent, :foreign_key => 'parent_id', :class_name => :Categoryto this model.Now, with everything in place, you can write real declarative code: