I have a model, called Procedure
which has to be owned by a user and assigned to another user. I have a single table User, with roles to distinguish users.
So a procedure has 2 references to the table User.
I found and implemented this solution here so in my Procedure model I have
belongs_to :owner, class_name: "User", foreign_key: "owner_id"
belongs_to :assignee, class_name: "User", foreign_key: "assignee_id"
and in model User I have this
has_many :owned_procedures, class_name: "Procedure", foreign_key: "owner_id"
has_many :assigned_procedures, class_name: "Procedure", foreign_key: "assignee_id"
but I cannot understand how this physically should be implemented, I mean at table level: does table procedures
need to have both the fields owner_id and assignee_id
, or just a field user_id
?
Does table procedures need to have both the fields owner_id and assignee_id, or just a field user_id
You should have both owner_id
and assignee_id
in procedures
table instead of user_id
so that you can call
@user.owned_procedures
@user.assigned_procedures
to get owned_procedures
and assigned_procedures
of a @user
which is an instance of User
. And to get a owner
and assignee
of a procedure, call
@procedure.owner
@procedure.assignee
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.