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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:26:25+00:00 2026-06-06T05:26:25+00:00

I am facing Following problem, since i m a newbie to ruby on rails

  • 0

I am facing Following problem, since i m a newbie to ruby on rails and not also i m not able to understand fully after_save callbacks, have got stuck

    class StoreOpeningStock < ActiveRecord::Base
     after_save :add_stock

     def add_stock
        s = Stock.find_by_product_id(self.product_id)
        if s.product_id?
        s.update_attributes(:product_id => self.product_id, :quantity => self.quantity, :price => self.price)
        else    
        Stock.create(:product_id => self.product_id, :quantity => self.quantity, :price => self.price)
  end
    end
    end

i am getting this as error

undefined method product_id

Basically just checking if Stock has a product? if yes.. do update else Create new Stock.. I Feel The problem is with s.product_id…but not sure..Any guidance on this subject would help alot…Thanks in advance.

  • 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-06T05:26:27+00:00Added an answer on June 6, 2026 at 5:26 am

    I believe the problem is with this chunk of code:

    if s.product_id?
    

    Problably the product_id? method doesn’t exist, and problably what you want is something like this:

    if s
    

    This checks if s exists in your database from your previous query. If s is nil then you create a new record.

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

Sidebar

Related Questions

Im facing following problem I have created mentioned condition, but when I choose y
I'm facing following problem actually: I'm using Spring with jQuery. I have Controller: @Controller
Our team (QA) is facing the following problem: We have a database that is
I am learning facelets and Seam and I'm facing the following problem: I have
I'm facing the following issue. I have a view with a UINavigationBar (not NavController)
I am facing the following problem. Let's say I have a file, which contains
I'm facing the following problem : I have to merge two images A and
I'm facing the following problem and don't have an answer to it: We have
I am facing the following problem : I have a namespace Exception\* , which
I'm facing the following problem: I have one module in my webapp that needs

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.