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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:58:57+00:00 2026-05-23T04:58:57+00:00

Say am writing a function that will add a product to the shopping cart.

  • 0

Say am writing a function that will add a product to the shopping cart.

I have a cart.rb model, and the method signature looks like:

def self.add_product(store, user, product, quantity, ...)
  # store.id == product.store_id
  # quantity > 0 ?
  # product is active?
  # if product is in cart, update quantity

end

So I have to pass in around 4 other models, and then to some sanity checks also.

So if store.id != product.store_id, I want to return with some kind of an error or status saying the product doesn’t belong to this store so I can’t continue.

If the quanitity is 0, I want to tell the user the quantity has to be > 0.

etc.

Where should all this logic be? There are many other models involved so I’m very confused.

Should I use the vote error collection? Or pass back status codes?

What is the rails way? Please clarify.

Thanks!

  • 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-05-23T04:58:57+00:00Added an answer on May 23, 2026 at 4:58 am

    To elaborate on my comment above, here’s how your Cart and CartItem classes might look/work.

    class Cart < ActiveRecord::Base
      has_many    :items, :class_name => 'CartItem'
      belongs_to  :user   # one user per cart 
      belongs_to  :store  # and one store per cart 
    end
    
    class CartItem < ActiveRecord::Base
      belongs_to :cart
      belongs_to :product
    
      validates_presence_of :cart, :product
    
      # sanity check on quantity
      validates_numericality_of :quantity,  :greater_than => 0,
                                            :only_integer => true
    
      # custom validation, defined below
      validate :product_must_belong_to_store
    
      protected
      def product_must_belong_to_store
        unless product.store_id == cart.store_id
          errors.add "Invalid product for this store!"
        end
      end
    end
    
    # Usage:
    
    # creating a new cart
    cart = Cart.create :store => some_store, :user => some_user
    
    # adding an item to the cart (since `cart` already knows the store and user
    # we don't have to provide them here)
    cart_item = cart.items.create :product => some_product, :quantity => 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a function that will take a parameter and, among other things,
Lets say we have a native library that works with data like this: double
I am writing a plugin. For that I will log a few things, say
Or, am I doing it wrong? I am writing a small function that will
so I have writing a function that returns objects on the stage and puts
I'm writing a function that will return database results. I need it to return
Lets say I am writing Dijkstra's Algorithm , and I have a priority queue
That is to say, let's say I'm writing something that's hosted on foo.com .
Say that you're writing a library to display things on the screen, so you
Lets say that I am writing a program that supports plugins and I write

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.