ruby on rails - How must I structure my models to connect owners, borrowers and products? -
i'm newbie in code, , i'm trying set 2 connected models : user model, , product model. product model has 2 user, 1 owner, , borrower. user model has many products, owner, , borrower.
do know if code below fulfilling purpose ?
class user has_many :products end class product belongs_to :owner, class_name: "user", foreign_key: "user_id" has_one :borrower, class_name: "user", foreign_key: "user_id" end
actually, need 2 different columns in product model "pointing" user model:
owner_id, borrower_id
you user model should following:
class user has_many :owned_products, class_name: "product", foreign_key: "owner_id" has_many :borrowed_products, class_name: "product", foreign_key: "borrower_id" end
and product model this:
class product belongs_to :owner, class_name: "user", foreign_key: "owner_id" belongs_to :borrower, class_name: "user", foreign_key: "borrower_id" end
Comments
Post a Comment