I'm still working on learning RSpec, so I'm sorry I completely missed something ...
I am writing a recipe test with many ingredients. The ingredients are actually added as a percentage (with the full column% in the wording), so I want to make sure that the total number of columns is updated after each save.
So now my RSpec test for the recipe_ingredient model has the following:
it "should update recipe total percent" do
@recipe = Factory.create(:basic_recipe)
@ingredient.attributes = @valid_attributes.except(:recipe_id)
@ingredient.recipe_id = @recipe.id
@ingredient.percentage = 20
@ingredient.save!
@recipe.total_percentage.should == 20
end
I have an after_save method that simply causes a quick update to the newly saved receipt instance. It is very simple:
EDIT: This update_percentage action is in the recipe model. The method that I call after saving the ingredient just looks at its recipe and then calls this method on it.
def update_percentage
self.update_attribute(:recipe.total_percentage, self.ingredients.calculate(:sum, :percentage))
end
- ? ? , , . , - , , .
/!