gpt4 book ai didi

ruby-on-rails - 正确的 rails 型号是什么?建议使用冗余?

转载 作者:行者123 更新时间:2023-12-04 06:04:22 25 4
gpt4 key购买 nike

在我的Rails应用程序中,Company充当用户模型。公司可以有许多客户和许多员工(汽车销售商)。

汽车销售商和客户之间存在多对多关系。

存在以下问题:很多时候,我不得不检索与整个公司所做的所有约会。由于在约会模型中没有保存company_id,这可能会很痛苦。

我是否应该在约会中仅包含公司的外键并进行某种形式的裁员,还是有另一种简便有效的方法?

class Company < ActiveRecord::Base  
has_many :customers
has_many :carseller
end

class Customer < ActiveRecord::Base
belongs_to :company

has_many :appointments
has_many :carsellers, :through => :appointments
end

class Carseller < ActiveRecord::Base
belongs_to :company

has_many :appointments
has_many :customers, :through => :appointments
end

class Appointment < ActiveRecord::Base
belongs_to :customer
belongs_to :carseller
end

最佳答案

我认为您可以使用:

class Appointment < ActiveRecord::Base
belongs_to :customer
belongs_to :carseller
has_one :company, :through => :carseller
end


然后,您只需要执行 appointment.company即可。

关于ruby-on-rails - 正确的 rails 型号是什么?建议使用冗余?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7092108/

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