gpt4 book ai didi

ruby-on-rails - 用户、公司和介绍之间的关系模型

转载 作者:数据小太阳 更新时间:2023-10-29 08:57:19 24 4
gpt4 key购买 nike

对于新手问题,我深表歉意,但我完全被这个问题难住了。

我有两个模型,用户和公司,我试图在它们之间建立“request_introduction”关系。

一个用户可以向许多公司提出一个 intro_request,而一个公司可以向多个用户提出一个 intro_request。

提前致谢。

最佳答案

布莱恩,听起来您想在“用户”和“公司”之间建立多对多关系。您可以通过创建 :through 关联来设置它。

应该是这样的:

class User < ApplicationRecord
has_many :introductions
has_many :customers, through: :introductions
end

class Customer < ApplicationRecord
belongs_to :users
belongs_to :customers
end

class Introduction < ApplicationRecord
has_many :users
has_many :customers, through: :users
end

这里是 Rails 指南的链接:(第 2.4 节)http://guides.rubyonrails.org/association_basics.html

关于ruby-on-rails - 用户、公司和介绍之间的关系模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48624891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com