【问题标题】:cancel association for inhereted STI model取消继承的 STI 模型的关联
【发布时间】:2015-01-18 20:33:02
【问题描述】:

我在名为 User 的 STI 父模型中设置了 has_many 关联,从 User 继承的模型是 Group,我不希望 Group 具有 has_many 关联,有没有办法做到这一点?

【问题讨论】:

  • 所以假设用户有_many 项目。当您执行“group.projects”时,您想要返回什么? “项目。无”?因为无法“取消”关联
  • 我想要返回 NoMethodError。我可以在关联定义上设置条件吗?

标签: ruby-on-rails ruby ruby-on-rails-4 activerecord


【解决方案1】:

以下应该有效:

class User < ActiveRecord::Base
  has_many :relations
end

class Group < User
  undef :relations
end

【讨论】:

  • 非常感谢,不知道“undef”,这很棒。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多