【问题标题】:What is the correct rails model? Using redundancy recommended?什么是正确的导轨型号?推荐使用冗余?
【发布时间】:2011-10-28 20:35:59
【问题描述】:

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

carsellercustomer 之间存在多对多关系。

存在以下问题:很多时候,我必须检索与整个公司进行的所有约会。由于 appointment 模型中没有保存 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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 database-design model entity-relationship


    【解决方案1】:

    我认为你可以使用:

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

    那么你只需要appointment.company 就可以得到它。

    【讨论】:

    • 谢谢。但更重要的是另一个方向:获取属于某个公司的所有约会
    • 然后,您在 Company 模型中执行相同操作,如下所示:has_many :carsellershas_many :apppointments, :through =&gt; :carsellers
    • 我就是这样尝试的。然后使用 company.appointments 访问它。但不幸的是它不起作用:-((#<0x000001027d19c0>
    猜你喜欢
    • 2019-03-30
    • 1970-01-01
    • 2011-11-29
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 2010-11-01
    相关资源
    最近更新 更多