从 has_many 关系继承范围
我正在使用 ruby on Rails 3.1,有 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 =>主组?我是否以错误的方式处理这个问题?
谢谢!
I am using ruby on rails 3.1 and have 2 models, an event and a group. Each event has_many groups, but has to have at least one "master" group, where the column :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
I want to be able to default all properties of the master group to the event, so for example, event.users.count should be the same as event.master_group.users.count.
Is there any way to do something like this? Can I do a has_many :through => master_group? Am I approaching this the wrong way?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我正在寻找的是代表
希望这可以帮助别人......
I think what I was looking for was delegate
hope this helps someone...