更新时间:2023-09-18 23:45:28
班的通知
belongs_to的:卖方,将class_name:用户
belongs_to的买方,将class_name:用户
结束
和您的通知表的外键应该是 seller_id
和 buyer_id
。
该foreign_keys的名称由协会的名称(卖方和买方在这里)的推断。
I know that if there is a "users" table and a "posts" table, the posts table has a foreign key column named 'user_id', according to the rails convention. My problem arises when I have a "users" table and the relation that i am trying to make is unary many-many(many buyers can notify many sellers, buyers and sellers are both "users"). Now "notifications" is the join table, that must contain the foreign keys "user_id" and "user_id" for storing the buyer and seller ids according to the rails convention of naming foreign keys, but obviously this cannot be done. Can someone tell me how to name F.K columns in a unary many-many relation and what are the rails accessor methods for such a relation?
class Notification
belongs_to :seller, class_name: "User"
belongs_to :buyer, class_name: "User"
end
And your foreign keys in the notifications table should be seller_id
and buyer_id
.
The names of the foreign_keys are inferred by the name of the associations (seller and buyer here).