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

  • SEARCH
  • Home
  • 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 8708827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:13:38+00:00 2026-06-13T04:13:38+00:00

I encountered a strange behavior when I run specs. This code doesn’t work (

  • 0

I encountered a strange behavior when I run specs.

This code doesn’t work (baz.viewed is not updated) unless I uncomment line baz.reload.

describe "#..." do
  it "..." do
    baz = user.notifications.create!(title: "baz")
    baz.update_attribute(:created_at, Time.now + 3.day)

    # it sets `viewed` to `true` in the model to which `baz` is referred.
    user.dismiss_latest_notification!

    # baz.reload
    baz.viewed.should == true
  end
end

I don’t run specs using Spork or Guard, but this model isn’t reloaded anyway.

Why it might happening? Or, is it normal practice to invoke .reload methods in specs?

  • 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-06-13T04:13:39+00:00Added an answer on June 13, 2026 at 4:13 am

    Let me make this case a bit clearer for you:
    When the line baz = user.notifications.create!(title: "baz") gets executed, two things happen:

    1- A new notification row is added to the DB.

    2- An object is created in memory, representing this row, and may be referenced with the variable baz.
    Note that baz has a viewed value of false (and so does the row in the meantime).

    Now I didn’t see the actual implementation of the method

    user.dismiss_latest_notification!
    

    But since you are not passing any variable to it I surely know you have a code in the spirit of:

    def dismiss_latest_notification!
      latest_notification = self.notifications.last
      latest_notification.viewed = true
      latest_notification.save!
    end
    

    The important line here is

       latest_notification = self.notifications.last
    

    An object is created in memory, representing the same row baz does, but stored in another variable – latest_notification.

    Now you have two variables representing the same row in DB. When you perform the save on latest_notification the DB is updated with the right viewed value but the variable baz is not updated in any manner to reflect this change. You have no choice but to force the update from the DB with the latest values by executing reload on it.

    I think the right way to get rid of the reloading is to change the test a little bit:

    Instead of

    baz.viewed.should == true
    

    Use:

    user.notifications.last.viewed.should be_true
    

    In my opinion, it better suits the purpose of this specific test.

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

Sidebar

Related Questions

I encountered some strange behavior which hints that I do not understand some basic
I have not programmed C++ for a while and encountered a strange behavior when
I encountered this strange behavior of MongoDB v2.0.7 when I was trying to use
I encountered a very strange behavior in Python, a behavior that is not consistent.
I encountered strange behavior of an JBoss AS 7 on linux server. When I
I encountered a very strange behavior when i m trying to insert a NSMutableDictonary
I've encountered (what I think is) a strange behavior when using the sax parser,
We have encountered a very strange class not found problem in our web app
I encountered strange behavior of enum type loaded by different class loader. In common
I've encountered a strange behavior in __declspec(dllexport) in my project. I have a C++

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.