【问题标题】:Polymorphic or has_many :through for recording cash flow多态或 has_many :through 用于记录现金流
【发布时间】:2012-08-23 19:32:24
【问题描述】:

到目前为止,我已经花了一天的时间尝试对以下内容进行建模,我认为这应该很简单,我已经看过太多了,可能有一个明显的解决方案在盯着我..

目标:Users 预订 clients 到可收费的 events 上,用户访问 cashbox 区域为那些 chargeable events(money in)、other_charges(money in) 的付款进行核算,但仅限与客户有关)和记录一般expenses(钱出)与事件或客户无关)。

到目前为止:使用正确关联创建的用户、客户端和事件模型。典型场景;用户将客户端预订到事件中,该事件可以收费(布尔值)。

# == Associations so far

class User < ActiveRecord::Base
  has_many :clients
  has_many :events
end

class Client < ActiveRecord::Base
  has_one :user, through: :events
  has_many :events
end

class Event < ActiveRecord::Base
  belongs_to :user
  belongs_to :client
end

问题:cashbox 的关联应该是多态的还是 has_many :through?

他们使用多态来理解它;事件、其他费用和费用可以是cashboxable。这样做的缺点是我知道我不能sum 处理多态关联?

通过has_many,我很困惑如何将其关联起来,这似乎很复杂。但我可能会遗漏一些东西。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 has-many-through polymorphic-associations


    【解决方案1】:
    class User < ActiveRecord::Base
      has_many :clients
      has_many :events
    end
    
    class Client < ActiveRecord::Base
      belongs_to :user, through: :events
      has_many :events
    end
    
    class Event < ActiveRecord::Base
      belongs_to :user
      belongs_to :client
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      • 2011-10-01
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 2010-12-13
      相关资源
      最近更新 更多