Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 906991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:28:38+00:00 2026-05-15T16:28:38+00:00

I’m still a working on learning RSpec so I’m sorry if completely overlooked something…

  • 0

I’m still a working on learning RSpec so I’m sorry if completely overlooked something…

I’m writing a test for a recipe that has many ingredients. The ingredients are actually added as a percent (with a total % column on the formulation) so I want to make sure that the total column updates after every save.

So right now my RSpec test for the recipe_ingredient model has something like this:

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 just calls a quick update on the just saved receipt ingredient. It’s very straightforward:

EDIT: This update_percentage action is in the recipe model. The method I call after I save an ingredient just looks up it’s recipe and then calls this method on it.

def update_percentage    
  self.update_attribute(:recipe.total_percentage, self.ingredients.calculate(:sum, :percentage))
end

Am I messing something up? Do I not have access to the parent object when running tests? I’ve tried to run a basic method to just change the parent recipe name after save but that didn’t work. I’m sure it’s something in the relationship I’ve overlooked, but all the relationships are setup correctly.

Thanks for any help/advice!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:28:39+00:00Added an answer on May 15, 2026 at 4:28 pm

    update_attribute is for updating the attributes of the current object. That means you need to call update_attribute on the object whose attribute you want to update. In this case, you want to update the recipe, not the ingredient. So you have to call recipe.update_attribute(:total_percentage, ...).

    Also, ingredients belong to recipes, not other ingredients. So instead of self.ingredients.sum(:percentage) you should really be calling recipe.ingredients.sum(:percentage).

    Also, you’ll need to reload @recipe before testing it’s total_percentage. Even though it refers to the same database record as @ingredient.recipe, it’s not pointing to the same Ruby object in memory, so updates to one won’t appear in the other. Reload @recipe to fetch the latest values from the database after saving the @ingredient.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.