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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:13:36+00:00 2026-06-10T06:13:36+00:00

I am trying to update another associated table before saving but i am getting

  • 0

I am trying to update another associated table before saving but i am getting into errors i am not familiar with…

so i have in my model

post.rb

  before_save :update_total_items

  def update_total_items
    b = Tags.find(:tag_id => self.tag_id)
    b.total_items = b.total_items+1
    b.save
  end

but i get an error

Unknown key tag_id

so how can i access the property i am adding before i save it in order to use it and update an existing associated on another table?

  • 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-10T06:13:37+00:00Added an answer on June 10, 2026 at 6:13 am

    Usually the id of a model is called just that. Generally what you want is:

    b = Tags.find(self.tag_id)
    

    Be aware this might throw an ActiveRecord::RecordNotFound exception if no record could be found. A safer approach is:

    if (b = Tags.where(:id => self.tag_id).first)
      b.increment!(:total_items)
    end
    

    Avoid loading and saving to increment a value. You may end up with an ugly race condition where you lose counts.

    As a note, it’s highly irregular that your model is called Tags. It should be called Tag.

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

Sidebar

Related Questions

I am trying to create a trigger to update another table with a condition
I am trying to update a field in a table with data from another
I'm trying to update one table based on values in another table. What's wrong
I'm trying to update the price of products in one table (ps_product) from another
I am trying to update a table in my database with another row from
I'm trying to update a field in one table, from the sum of another
I'm trying to update a table based upon the user id from another table.
I'm trying to update a column in a table based on another column in
I am trying to update a table by joining the values with another table.
I have a problem trying to update an attribute of one model using the

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.