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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:49:07+00:00 2026-06-08T00:49:07+00:00

Before everything i would like to thank you for your help I have a

  • 0

Before everything i would like to thank you for your help

I have a model like this:

  attr_protected nil
  belongs_to :product
  belongs_to :user
  before_create :add_ammount

  def carted_product_price(ammount, price)
    ammount * price
  end

  def add_ammount
    carted_product = CartedProduct.where(:product_id => self.product_id, :user_id => self.user_id)
    if carted_product
      carted_product.first.ammount += self.ammount
      carted_product.first.update_attributes(:ammount => carted_product.first.ammount)
    else
      self.save
    end
  end

it saves buying orders in a table called Carted_Products connected to Users and Products in the belogings

the problem is that when the Before create executes i want it to update the record in the table adding the ammount passed by the controller if the record already exists and if not, create one, as far as iv done, it updates the ammount but STILL CREATES A NEW one with the passed params in the order, i want it only to update, not to do both actions when the record is found

thnx for your patience

EDIT:

Tried returning false after the update attributes, it cancels the filter, and dont create or update attributes

  • 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-08T00:49:09+00:00Added an answer on June 8, 2026 at 12:49 am

    Return false in the before_create filter to prevent the object form being saved. add_amount is not responsible for saving the object, and shouldn’t call save by itself.

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

Sidebar

Related Questions

Before I go into doing everything by hand I would like to ask if
...before everything, I'm doing this out of curiosity only. Nothing real-world application here, but
before everything, ty for helping and sorry for the noob question. I have problem
If you read this thread before - forget everything I wrote , I must
I have a simple xml file and I want to remove everything before the
I have a string 2500 - SomeValue. How can I remove everything before the
I have files with the words written on different speaking languages. I would like
Before I post my question I would like to tell you that I'm aware
I have a MySQL database with a PHP front-end. I would like to implement
I use NAudio. Before everything was good. But yesterday there was an error: All

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.