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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:10:35+00:00 2026-06-16T11:10:35+00:00

My Post model has the following columns: t.datetime published_at t.string status published_at Should only

  • 0

My Post model has the following columns:

t.datetime "published_at"
t.string   "status"

published_at Should only be defined once (the first time the post.status equals "Published"):

post.rb:

 before_save :publish_post

 protected

    def publish_post
      if self.status == "Published" && self.published_at.nil?
       self.published_at = Time.now
      end
    end

Now, I added a posts_count column to the Tag model:

t.integer  "posts_count", :default => 0, :null => false

The counter should only increment and decrement if the post.status equals "Published" (Shouldn’t increment is post.status equals "Draft"):

taggings.rb:

  after_save    :increment_tag_counter_cache
  after_destroy :decrement_tag_counter_cache

  private

   def increment_tag_counter_cache
     if self.post.status == "Published" && self.post.published_at.nil?
       Tag.increment_counter(:posts_count, self.tag.id)
     end
   end

   def decrement_tag_counter_cache
      if self.post.status == "Published" && self.post.published_at.nil?
        Tag.decrement_counter(:posts_count, self.tag.id)
      end
    end

For some reason, the right now the counter is not being incremented or decremented. I tried removing code and discovered that the problem is this part: self.post.published_at.nil?.

I’m not very sure what’s going on. I’m pretty sure published_at is nil at that moment. What could be the problem?

  • 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-16T11:10:41+00:00Added an answer on June 16, 2026 at 11:10 am

    How do you know that

    post.status == "Published" && post.published_at is really nil?
    

    ?

    post.published_at can be just blank. like “”

    post.published_at.blank?
    

    Examples of .blank? and .nil?:

    "".nil?
    #=> false
    
    nil.nil?
    #=> true
    
    nil.blank?
    #=> true
    
    "".blank?
    #=> true
    
    "  ".blank?
    #=> true
    
    "     ".blank?
    #=> true   
    
    [].blank?
    #=> true
    
    {}.blank?
    #=> true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Post and Comment model. I want to achieve the following: Each time
I have a Topic model and a Post model. A Topic has many Posts,
My Post model in schema.yml (Symfony 1.4) looks like this: Post: columns: id: {
I have an user view model that has the following properties: public User user;
Update following initial comments The model has an object in it called 'Account', of
I've converted a string property in my model to a class that has implicit
I have a model that extends CActiveRecord that has the following rules() method: /**
I am doing an user crud in spring-mvc. My model has the following properties:
I have a Post model which has many ratings and I would like to
I have a Post model that has a polymorphic association with a Vote model:

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.