【问题标题】:inheriting scope from a has_many relationship从 has_many 关系继承范围
【发布时间】:2011-02-25 23:22:04
【问题描述】:

我在 Rails 3.1 上使用 ruby​​,有 2 个模型,一个事件和一个组。每个事件有_many 个组,但必须至少有一个“主”组,其中列 :is_master => true

Class Group < ActiveRecord::Base
  has_many :users
  belongs_to :event
  scope :master, where (:is_master => true)
end

Class Event< ActiveRecord::Base
  has_many :groups
  def master_group
     groups.master
  end
end

我希望能够将主组的所有属性默认为事件,例如,event.users.count 应该与 event.master_group.users.count 相同。

有没有办法做这样的事情?我可以做一个 has_many :through => master_group 吗?我是不是走错了路?

谢谢!

【问题讨论】:

    标签: ruby-on-rails-3 scope associations has-many


    【解决方案1】:

    我想我要找的是委托

     delegate :users, :to => :master_group
    

    希望这对某人有所帮助...

    【讨论】:

      猜你喜欢
      • 2014-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      相关资源
      最近更新 更多