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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:53:51+00:00 2026-06-15T13:53:51+00:00

Im trying to use the call back after_find in my model, I’m having issues

  • 0

Im trying to use the call back after_find in my model, I’m having issues trying to get it to actually update the rows that it found in the after_find method. It’s throwing a no method error

error

Completed 500 Internal Server Error in 299ms

ActionView::Template::Error (undefined method `+' for nil:NilClass):
    1: <div id="hashtags" class="twitter-hashtag-voting-block-v1">
    2: <% @random_hashtag_pull.each do |hashtag| %>
    3: <div class="span4 twitter-spans-v1" id="<%= hashtag.id %>">
    4:      <div id="tweet-block-v1" class="hashtag-tweet-database-container">
    5:      <div class="tweet-block-border-v1">
  app/models/hashtag.rb:46:in `update_view_count'
  app/views/shared/_vote_tweets.html.erb:2:in `_app_views_shared__vote_tweets_html_erb__2738953379660121418_70243350609340'
  app/views/hashtags/create.js.erb:2:in `_app_views_hashtags_create_js_erb___1440072038737667206_70243345272440'
  app/controllers/hashtags_controller.rb:23:in `create'

hashtag_controller

class HashtagsController < ApplicationController
  def home 
  end
  def vote
    @random_hashtags = Hashtag.order("RANDOM()").limit(4)
  end
  def show
  end
  def index
  end
  def create 
    Hashtag.pull_hashtag(params[:hashtag])
    @random_hashtag_pull = Hashtag.random_hashtags_pull
    respond_to do |format|
      format.html { redirect_to vote_path }
      format.js
    end
  end
end

hashtag.rb

class Hashtag < ActiveRecord::Base

attr_accessible :text, :profile_image_url, :from_user, :created_at, :tweet_id, :hashtag, :from_user_name, :view_count

after_find :update_view_count

def self.pull_hashtag(hashtag)
  dash = "#"
  @hashtag_scrubbed = [dash, hashtag].join
  Twitter.search("%#{@hashtag_scrubbed}", :lang => "en", :count => 100, :result_type => "mixed").results.map do |tweet|
    unless exists?(tweet_id: tweet.id)
      create!(
        tweet_id: tweet.id,
        text: tweet.text,
        profile_image_url: tweet.user.profile_image_url,
        from_user: tweet.from_user,
        from_user_name: tweet.user.name, 
        created_at: tweet.created_at,
        hashtag: @hashtag_scrubbed
      ) 
      end       
    end
  end

  def self.random_hashtags_pull
    Hashtag.where{ |hashtag| hashtag.hashtag =~ @hashtag_scrubbed}.order{"RANDOM()"}.limit(4)
  end

  def update_view_count
    count = (view_count + 1)
    view_count = count
    save!
  end

end
  • 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-15T13:53:52+00:00Added an answer on June 15, 2026 at 1:53 pm

    You have two problems here, one you know about and one you probably don’t know about.

    The first problem is that view_count has no default value so it starts out as nil. So the first time you try to update the view_count, you end up doing:

    count = nil + 1
    

    and nil doesn’t know what + means. Calling nil.to_i gives you zero so you can do this:

    count = view_count.to_i + 1
    

    The other problem is that you have a race condition. If two processes end up viewing the same thing at the same time then you can end up with this sequence of events:

    1. Process one (P1) pulls view_count out of the database.
    2. Process two (P2) pulls view_count out of the database.
    3. P1 sends view_count+1 back into the database.
    4. P2 sends view_count+1 back into the database but it won’t include the increment from 3.

    The easiest way to solve this is to use increment_counter:

    def update_view_count
      Hashtag.increment_counter(:view_count, self.id)
    end
    

    That will do a direct

    update hashtags set view_count = coalesce(view_count, 0) + 1
    

    in the database so the race condition goes away as does the nil problem. You could also include a reload if you wanted to have an up-to-date view_count or just add one and don’t save the modified Hashtag:

    def update_view_count
      Hashtag.increment_counter(:view_count, self.id)
      self.reload
    end
    # or
    def update_view_count
      Hashtag.increment_counter(:view_count, self.id)
      self.view_count += 1 # And don't save it or you'll overwrite the "safe" value!
    end
    

    The first one (with self.reload) will cause problems when tied to an after_find callback: the self.reload will probably trigger the after_find callback which will trigger another self.reload which will trigger the callback … until Ruby starts getting upset about infinite recursion. But, it should work fine if you manually call update_view_count rather than tying it to a callback (see below).

    The self.view_count += 1 version can leave out some increments but that’s probably not a big deal as you’ll always have room for missing increments (unless you have live-updating on the view counts of course).

    I don’t think using a callback is a good idea for this sort of thing though. There will be times when you load a Hashtag out of the database but you don’t want the view_count to increment. You’d be better off requiring an explicit method call to increment the counter, that way you won’t increment things accidentally. Requiring an explicit call would allow you to use the first version (with self.reload) of update_view_count above as you wouldn’t have the callback triggering the infinite recursion.

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

Sidebar

Related Questions

We're trying to use Axis2 to call a web service that cannot use HTTP/1.1
I am trying to update the Screen on a event Call back in blackberry
I am trying to use Twilio to call a phone number. I was looking
I'm trying to use CallFunction() to call a function inside a flash file, but
I am trying to use to a PUT call to a API using JSON.
I am trying to use a string to call a method? Suppose I have
I'm trying to use require_once to call Mail.php but when I create a new
I am trying to use relative url with a post ajax call as follows:
I'm trying to use JQuery's $.getJSON to access Model data from the Controller, and
I'm trying to use hibernate to build up a local cache of data that

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.