class RecipeIngredient < ActiveRecord::Base belongs_to :ingredient belongs_to :recipe, inverse_of: :recipe_ingredients validates :sort_order, presence: true def name if self.ingredient_id.present? self.ingredient.name else super end end def display_name str = [quantity, units, name].delete_if { |i| i.blank? }.join(' ') str << ", #{preparation}" if preparation.present? str end def scale(factor, auto_unit = false) if factor.present? && self.quantity.present? && factor != '1' self.quantity = UnitConversion.convert(self.quantity, factor, nil, nil) if auto_unit self.quantity, self.units = UnitConversion.auto_unit(self.quantity, self.units) end end end end