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

The Archive Base Latest Questions

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

I have three models: User, Product, Offer and a problem with the relationship between

  • 0

I have three models: User, Product, Offer and a problem with the relationship between these models.

Scenario:

User 1 posts a product

User 2 can send User 1 an offer with an price e.g $ 10

User 1 can accept or reject the offer

My questions are now:

What is the right relationship between User, Product and Offer?

How can I handle those “accept or reject” actions?

Is there maybe a better solution?

User model:

class User < ActiveRecord::Base
    attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :avatar, :screen_name
    has_many :products
    has_many :offers,:through => :products
end

Product model:

class Product < ActiveRecord::Base
    attr_accessible :content, :price, :title, :tag_list, :productimage, :user_id
    belongs_to :user
    has_many :offers, :through => :users
end

Offer model:

class Offer < ActiveRecord::Base
    attr_accessible :offer_price, :status, :user_id, :product_id
    has_many :products
    has_many :users, through: :products
end

Thanks in advance 🙂

EDIT:

I am using Rails 3.2.8

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

    Warning: here comes a small novel.

    Part 1: setting up the associations

    I’d recommend reading the Rails guide on associations thoroughly, bookmark it, and read it again, because this is a key thing to understand properly, and can be a bit tricky – there are lots of options once you go beyond basic associations.

    One thing to notice about your app is that your users have two roles, buyers and sellers. You’re going to need to be careful with the names of your associations – Does @user.offers return the offers the user has made, or the offers the user has received? You might want to be able to put lists of both these things in the user’s profile.

    The basic relationships you’re describing are fairly simple:

    • A user can sell many products, so User has_many :products and Product belongs_to :user

    • A user can make many offers, so User has_many :offers and Offer belongs_to :user

    • A product may receive many offers so Product has_many :offers and Offer belongs_to :product

    That’s all well and good, and you could certainly get by just doing this – in which case you can skip down to Part 2 🙂

    However, as soon as you start trying to add the through relationships the waters are going to get muddy. After all,

    • Offer belongs_to :user (the buyer), but it also has a user through product (the seller)

    • User has_many :products (that they are selling), but they also have many products through offers (that they are buying – well, trying to buy).

    Aargh, confusing!

    This is the point when you need the :class_name option, which lets you name an association differently to the class it refers to, and the :source option, which lets you name associations on the ‘from’ model differently to the ‘through’ model.

    So you might then form your associations like this:

    # User
    has_many :products_selling, class_name: 'Product'
    has_many :offers_received, class_name: 'Offer',
             through: :products_selling, source: :offers
    
    has_many :offers_made, class_name: 'Offer'
    has_many :products_buying, class_name: 'Product',
             through: :offers_made, source: :product
    
    
    # Product
    belongs_to :seller, class_name: 'User', foreign_key: :user_id
    has_many :offers
    has_many :buyers, class_name: 'User', through: :offers
    
    # Offer
    belongs_to :product
    belongs_to :buyer, class_name: 'User', foreign_key: :user_id
    has_one :seller, class_name: 'User', through: :product
    

    Although if you renamed your user_id columns to seller_id in the products table, and buyer_id in the offers table, you wouldn’t need those :foreign_key options.

    Part 2: accepting/rejecting offers

    There’s a number of ways to tackle this. I would put a boolean field accepted on Offer and then you could have something like

    # Offer
    def accept
      self.accepted = true
      save
    end
    
    def reject
      self.accepted = false
      save
    end
    

    and you could find the outstanding offers (where accepted is null)

    scope :outstanding, where(accepted: nil)
    

    To get the accept/reject logic happening in the controller, you might consider adding new RESTful actions (the linked guide is another one worth reading thoroughly!). You should find a line like

    resources :offers
    

    in config/routes.rb, which provides the standard actions index, show, edit, etc. You can change it to

    resources :offers do
      member do
        post :accept
        post :reject
      end
    end
    

    and put something like this in your OffersController

    def accept
      offer = current_user.offers_received.find(params[:id])
      offer.accept
    end
    
    # similarly for reject
    

    Then you can issue a POST request to offers/3/accept and it will cause the offer with id 3 to be accepted. Something like this in a view should do it:

    link_to "Accept this offer", accept_offer_path(@offer), method: :post 
    

    Note that I didn’t just write Offer.find(params[:id]) because then a crafty user could accept offers on the behalf of the seller. See Rails Best Practices.

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

Sidebar

Related Questions

I have three models. User, Product, and Season. I used a standard has many
I have three models ( User , Tag , Product ) and they interact
I have three models: User Image Group . User belongs to an image; Group
I have three models: class User include Mongoid::Document field :name, :type => String has_many
I have three models: Post , Comment , User and Vote . I'm using
Let's say I have these tables/models: Product - id - last_updated_date - name -
I have the following two models: class Product < ActiveRecord::Base belongs_to :shop validates_numericality_of :price,
I have a Product edit screen. The user can select a Vendor for the
I have three models that are coming together to create one view model and
I have three models that I'm trying to setup: Location/Venues, Categories, and Neighborhoods. A

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.