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 8564641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:14:42+00:00 2026-06-11T17:14:42+00:00

I’ve added a very simple counter to my post model. Whenever someone open a

  • 0

I’ve added a very simple counter to my post model. Whenever someone open a post, it updates the corresponding field :counter by 1.

  def show
    @story = Story.find(params[:id])
    @comment = @story.comments.build
    @comments = @story.comments.arrange(order: :created_at)

    @story.increment!(:counter) # ---Here---

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @story }
    end
  end

Actually, I checked the DB and this thing is working. But my test get fail:

it "raise the counter by one when visiting a page" do
  story = FactoryGirl.create(:story)
  expect {
    visit story_url(story)
    }.to change { story.counter }.by(1)
end

And it shows:

1) Stories raise the counter by one when visiting a page
  Failure/Error: expect {
    result should have been changed by 1, but was changed by 0
    # ./spec/requests/stories_spec.rb:8:in `block (2 levels) in <top (required) >'

here is my test.log:

Started POST "/stories" for 127.0.0.1 at 2012-09-22 11:31:32 +0330
Processing by StoriesController#create as HTML
  Parameters: {"utf8"=>"✓", "story"=>{"title"=>"sfomqd8s2r", "content"=>"Beatae voluptatibus quia cumque voluptate. Beatae sed aut sit. Fugiat deleniti et vitae accusantium blanditiis. Consequatur at itaque temporibus autem. Quo beatae delectus minus porro et.", "tag_names"=>""}, "commit"=>"submit"}
   (0.1ms)  SELECT MIN("tags"."stories_count") AS min_id FROM "tags" 
   (0.1ms)  SELECT MAX("tags"."stories_count") AS max_id FROM "tags" 
  Tag Load (0.1ms)  SELECT name, stories_count FROM "tags" ORDER BY name asc
   (0.1ms)  SAVEPOINT active_record_1
  SQL (1.5ms)  INSERT INTO "stories" ("comments_count", "content", "counter", "created_at", "publish_date", "slug", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)  [["comments_count", 0], ["content", "Beatae voluptatibus quia cumque voluptate. Beatae sed aut sit. Fugiat deleniti et vitae accusantium blanditiis. Consequatur at itaque temporibus autem. Quo beatae delectus minus porro et."], ["counter", 0], ["created_at", Sat, 22 Sep 2012 11:31:32 IRST +03:30], ["publish_date", nil], ["slug", nil], ["title", "sfomqd8s2r"], ["updated_at", Sat, 22 Sep 2012 11:31:32 IRST +03:30], ["user_id", nil]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
Redirected to http://www.example.com/stories/2
Completed 302 Found in 18ms (ActiveRecord: 1.9ms)


Started GET "/stories/2" for 127.0.0.1 at 2012-09-22 11:31:32 +0330
Processing by StoriesController#show as HTML
  Parameters: {"id"=>"2"}
   (0.1ms)  SELECT MIN("tags"."stories_count") AS min_id FROM "tags" 
   (0.1ms)  SELECT MAX("tags"."stories_count") AS max_id FROM "tags" 
  Tag Load (0.1ms)  SELECT name, stories_count FROM "tags" ORDER BY name asc
  Story Load (0.1ms)  SELECT "stories".* FROM "stories" WHERE "stories"."id" = ? LIMIT 1  [["id", "2"]]
  CACHE (0.0ms)  SELECT "stories".* FROM "stories" WHERE "stories"."id" = ? LIMIT 1  [["id", "2"]]
  Comment Load (0.2ms)  SELECT "comments".* FROM "comments" WHERE "comments"."story_id" = 2 ORDER BY (case when comments.ancestry is null then 0 else 1 end), comments.ancestry, created_at
  SQL (0.3ms)  UPDATE "stories" SET "counter" = 1 WHERE "stories"."id" = 2
  Tag Load (0.2ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."story_id" = 2
  Rendered stories/_story_operation.html.erb (3.5ms)
  Rendered stories/_story.html.erb (5.4ms)
  Rendered comments/_form.html.erb (7.1ms)
Completed 200 OK in 34ms (Views: 18.1ms | ActiveRecord: 1.1ms | Solr: 0.0ms)
   (0.7ms)  rollback transaction

I can’t understand whats happening. Maybe it’s something about Capybara or the way I’ve wrote the test.

  • 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-11T17:14:43+00:00Added an answer on June 11, 2026 at 5:14 pm

    As you know,

    expect {
      visit story_url(story)
    }.to change { story.counter }.by(1)
    

    compares the value of story.counter before and after the block is executed. When your controller loads and modifies the story, that is a completely separate ruby object (that happens to refer to the same row in the database): the story object in your spec code is unaware of the changes made to the database and holds the same (now stale) data.

    To get your spec passing you could instead check whether story.reload.counter is changing.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to show the soap response to UIWebview.. my soap response is, <p><img
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm trying to create an if statement in PHP that prevents a single post
I'm making a simple page using Google Maps API 3. My first. One marker

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.