简体   繁体   中英

Two references of the same model in another Rails: 4

I ask if it is good to have two references of one model in another and if it can do it how do I do it?

Problem: I have a record that will be controlled by two users (both always different users) and I need to have them see the records they have.

picture example: example

The idea is that both can have access to the same registry and no one else can

you are trying to achieve many to many association, I would suggest you to use has_many_through association. you can read about it over here .

your tables will be like:

User:

id, email, name and other fields related to user

Box:

id, name and Fields related to boxes

User_Boxes: (Join table for mapping users with boxes, to do what you're trying to achieve)

id, user_id, box_id

so, according to your diagram:

let the id's of boxes in box table be:

  1. box home
  2. box 2
  3. box closet
  4. box keys

Your mapping table would look like:

id   box_id   user_id

1       1      1
2       1      3 
3       2      5
4       2      9
5       3      1
6       3      9
7       4      7
8       4      2

I hope this will solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM