0

I'm a newbie in code, and I'm trying to set up two connected models : A user model, and a product model. The product model has two user, one is the owner, and another is the borrower. The user model has many products, as owner, and as borrower.

Do you know if the code below is fulfilling my 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
Flo Rahl
  • 1,044
  • 1
  • 16
  • 33

2 Answers2

2

Actually, you need two different columns in your Product model "pointing" to your User model:

owner_id, borrower_id

You User model should be something like the 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 your Product model like this:

class Product
    belongs_to :owner, class_name: "User", foreign_key: "owner_id"
    belongs_to :borrower, class_name: "User", foreign_key: "borrower_id"
end
Lazarus Lazaridis
  • 5,803
  • 2
  • 21
  • 35
  • Thanks for your answer ! Can you tell me how I can connect the owner_id and borrower_id to my user_id in that case ? Must I have a relationship model between them ? – Flo Rahl May 14 '13 at 08:07
  • No you don't need a relationship model, you must add two columns to your Product model using a migration and remove the user_id one since you won't need it anymore. You will have the owner through the owner_id and the borrower through the borrower_id. – Lazarus Lazaridis May 14 '13 at 08:26
  • And where will be the technical differences between your answer and the answer of @Galen ? – Flo Rahl May 14 '13 at 08:33
  • You won't need to extend the User model. – Lazarus Lazaridis May 14 '13 at 08:36
  • You mean I can do the same things whether the solution I choose ? The only difference will be aesthetic ? – Flo Rahl May 14 '13 at 08:49
  • Business wise, do you need extra models for borrowers/owners? Or they are just users? If you had to add addresses to a person, would you need different classes for HomeAddress, WorkAddress etc? – Lazarus Lazaridis May 14 '13 at 08:53
  • Owners and Borrowers are just Users. So no need for extra class, because they share the same structure, I understand. – Flo Rahl May 14 '13 at 08:57
1

You can use an STI approach (Single Inheritance Table):

model user:

class User < ActiveRecord::Base
   # general attributes and validations and the like
end

owner model:

class Owner < User
  # specific attributes and/or validations if any
  has_many :products
end

borrower model:

class Borrower < User
  # specific attributes and/or validations if any
  haas_many :products
end

products model:

class Product < ActiveRecord::Base
  # attributes, validation and the like
  belongs_to :owner
  belongs_to :borrower
end

Basically that organizes Owner and Borrower as User types, inheriting its attributes.

one_owner.products will show you the products owned by one_owner

one_borrower.products will show you the products borrowed by one_borrower

one_product.owner will show you the owner for that product

one_product.borrower will show you the borrower for that product

You can see an extensive example in this thread: Rails Question: belongs_to with STI -- how do i do this correctly?

Community
  • 1
  • 1
Galen
  • 957
  • 5
  • 16
  • Thanks for your answer ! Just another question about this : Can a user be borrower and owner ? Or is it exclusive ? – Flo Rahl May 14 '13 at 08:06
  • I created two users and it made something funny. When I do `User.all` or `Borrower.all` or `Owner.all`, the result is the same. – Flo Rahl May 14 '13 at 08:21
  • @FloRahl, if you do `my_borrower = Borrower.new`, etc and `my_owner = Owner.new` you will have 2 for User.count, and 1 for each Borrower and Owner count (or .all). – Galen May 14 '13 at 08:46