2

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?

2 Answers 2

5
  1. Your method gets stuck because it has the same name than the attribute (so it's calling itself), pick a different name.
  2. Notice that this conditional is nothing more than a simple a || b expression.
  3. You should add a belongs_to :parent, :foreign_key => 'parent_id', :class_name => :Category to this model.

Now, with everything in place, you can write real declarative code:

def safe_future_purchases_percent
  future_purchases_percent || parent.safe_future_purchases_percent
end
Sign up to request clarification or add additional context in comments.

2 Comments

Think you got an extra end in there. Looks bueno otherwise.
Thanks @Andrew, that indentation level seemed to be asking for an end :-)
0
  def repeat_purchases_percent
    if self.future_purchases_percent.nil?
      Category.find(self.parent_id).repeat_purchases_percent
    else
      future_purchases_percent
    end
  end

Basically your problem is that you named your method the same thing as the field and then immediately call it. You should instead name the method like you said repeat_purchases_percent and only recurse into it when the future_purchases_percent is nil.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.