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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:48:35+00:00 2026-06-08T12:48:35+00:00

I have a count column in my tags table. I wanna increment tag count

  • 0

I have a count column in my tags table. I wanna increment tag count if a tag is just added to the post while updating, and it’s already inside db. I added this to my post model:

before_update :increment_tag

def increment_tag
  db_post = Post.find_by_id(self)
  self.tags.each do |tag|
    unless db_post.tags.include? tag
      tag.update_attribute("count", tag.count + 1)
    end
  end
end

I get the post from db and test if the current tag is already in db, if it is, nothing happens, if it’s not there, it should update count field. But for some reason this doesn’t work.

  • 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-08T12:48:36+00:00Added an answer on June 8, 2026 at 12:48 pm

    You should not have a Count column in a tag. You should in stead set up your models propperly so you could do the following:

    db_post.tags.count
    

    If you do it right, you can get this in your tag:

    tag.post.tags.count
    

    If your aim is to find how many times the tag is used in a post, in total, you can simply count the instances in the TagToPostColumn (if you got one), which you need to sine this is a many-to-many relation.

    Then you do:

    TagToPostColumn.where(tag_id: someTag.id).count

    count is a standard attribute, and you should never have to keep track of this yourself, unless you actually need a column called count that tracks something other than the models you have in the database. But then it is a good idea to name it something else than count, since it can lead to ambiguous attributes.

    Also, i find it very strange that you are doing this:

    db_post = Post.find_by_id(self)

    Why are you not just using the self parameter, in stead of doing a db lookup to find the post you already have.

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

Sidebar

Related Questions

I have a table that has a view_count column. I would like to increment
I have data with no relation. Just need to count various columns from 3
if i have a table which has columns with fixed lenght, Will mySQL count
Does COUNT(*) have any significant impact for MySQL performance if query already has GROUP
i have got a problem with my SQL Statement: Table structure: CREATE TABLE tags
With reference to this post: How to implement tag counting I have implemented the
I have a table in my database that looks like this. id count -----------
I have 2 tables in the DB. each with Name column and Count column
I have my tags desinged like this in my database: Table: Item Columns: ItemID,
I want to implement SO like tags on userpost. I have a table called

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.