Product orders between 2 users

后端 未结 3 1055
离开以前
离开以前 2021-01-06 02:29

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

Scenario:

User 1 posts a product

3条回答
  •  清酒与你
    2021-01-06 03:17

    Your models are good enough, except for the relations. The confusion starts when you are trying to differentiate the owned products vs interested products(offered) and product owner vs interested users(users who placed the offer). If you can come up with a better naming convention, you can easily fix it.

    1. Better relations

    class User < ActiveRecord::Base
      attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :avatar, :screen_name
      has_many :owned_products, :class_name => "Product"
      has_many :offers 
      has_many :interested_products, :through => :offers
    end
    
    class Offer < ActiveRecord::Base
      attr_accessible :offer_price, :status, :user_id, :product_id
      belongs_to :interested_user, :class_name => "User", :foreign_key => :user_id
      belongs_to :interested_product, :class_name => "Product", :foreign_key => :product_id
    end
    
    class Product < ActiveRecord::Base
      attr_accessible :content, :price, :title, :tag_list, :productimage, :user_id
      belongs_to :owner, :foreign_key => :user_id, :class_name => "User"
      has_many :offers 
      has_many :interested_users, :through => :offers
    end
    

    With these relations I think you can get all the basic information you would be interested. For Example,

    @product = Product.find(1)
    @product.owner # would give you the user who created the product
    @product.interested_users # would give you users who placed an offer for this product
    
    @user = User.find(1)
    @user.owned_products # would give you the products created by this user
    @user.interested_products # would give you the products where the user placed an offer
    

    2. Handling accept and reject actions.

    From your description, I see there can be 2 possible state changes to an offer, "created" -> "accept" or "created" -> "reject". I suggest you to look at state_machine. State machine will add nice flavor to your model with its helper methods, which I think will be very useful in your case. So your Offer model will look something like this,

    class Offer < ActiveRecord::Base
      # attr_accessible :title, :body
      attr_accessible :offer_price, :status, :user_id, :product_id
      belongs_to :interested_user, :class_name => "User", :foreign_key => :user_id
      belongs_to :interested_product, :class_name => "Product", :foreign_key => :product_id
    
      state_machine :status, :initial => :created do
        event :accept do
          transition :created => :accepted
        end
        event :reject do
          transition :created => :reject
        end
      end
    end
    
    #cool helper methods
    @offer = Offer.new
    @offer.accepted? #returns false
    @offer.reject #rejects the offer
    @offer.rejected? #returns true
    

    I hope this gives you a better picture.

提交回复
热议问题