【问题标题】:Best practices for multiple associations with the same class in Rails?Rails 中同一个类的多个关联的最佳实践?
【发布时间】:2009-04-16 18:42:51
【问题描述】:

我认为我的问题最好作为一个例子来描述。假设我有一个名为“Thing”的简单模型,它有一些简单数据类型的属性。比如……

Thing
   - foo:string
   - goo:string
   - bar:int

这并不难。 db 表将包含具有这三个属性的三列,我可以使用 @thing.foo 或 @thing.bar 之类的内容访问它们。

但我要解决的问题是,当“foo”或“goo”不再包含在简单数据类型中时会发生什么?假设 foo 和 goo 代表相同类型的对象。也就是说,它们都是“Whazit”的实例,只是具有不同的数据。所以现在事情可能看起来像这样......

Thing
  - bar:int

但是现在有一个叫做“Whazit”的新模型看起来像这样......

Whazit
  - content:string
  - value:int
  - thing_id:int

到目前为止,一切都很好。现在这是我卡住的地方。如果我有@thing,我该如何设置它以按名称引用我的 2 个 Whazit 实例(为了记录,“业务规则”是任何事物总是有 2 个 Whazit)?也就是说,我需要知道我拥有的 Whazit 基本上是 foo 还是 goo。显然,我不能在当前设置中执行 @thing.foo,但我希望这是理想的。

我最初的想法是为 Whazit 添加一个“名称”属性,这样我就可以获取与我的 @thing 关联的 Whatzits,然后通过这种方式选择我想要的 Whazit。不过这看起来很丑。

有没有更好的办法?

【问题讨论】:

    标签: ruby-on-rails ruby activerecord


    【解决方案1】:

    有几种方法可以做到这一点。首先,您可以设置两个belongs_to/has_one 关系:

    things
      - bar:int
      - foo_id:int
      - goo_id:int
    
    whazits
      - content:string
      - value:int
    
    class Thing < ActiveRecord::Base
      belongs_to :foo, :class_name => "whazit"
      belongs_to :goo, :class_name => "whazit"
    end
    
    class Whazit < ActiveRecord::Base
      has_one :foo_owner, class_name => "thing", foreign_key => "foo_id"
      has_one :goo_owner, class_name => "thing", foreign_key => "goo_id"
    
      # Perhaps some before save logic to make sure that either foo_owner
      # or goo_owner are non-nil, but not both.
    end
    

    另一个更简洁但在处理插件等时也更痛苦的选项是单表继承。在这种情况下,您有两个类,Foo 和 Goo,但它们都保存在 whazits 表中,并带有一个区分它们的类型列。

    things
      - bar:int
    
    whazits
      - content:string
      - value:int
      - thing_id:int
      - type:string
    
    class Thing < ActiveRecord::Base
      belongs_to :foo
      belongs_to :goo
    end
    
    class Whazit < ActiveRecord::Base
      # .. whatever methods they have in common ..
    end
    
    class Foo < Whazit
      has_one :thing
    end
    
    class Goo < Whazit
      has_one :thing
    end
    

    在这两种情况下,您都可以执行@thing.foo@thing.goo 之类的操作。使用第一种方法,您需要执行以下操作:

    @thing.foo = Whazit.new
    

    而使用第二种方法,您可以执行以下操作:

    @thing.foo = Foo.new
    

    不过,STI 也有其自身的一系列问题,尤其是在您使用较旧的插件和 gem 的情况下。通常这是调用@object.class 的代码的问题,而他们真正想要的是@object.base_class。必要时很容易修补。

    【讨论】:

      【解决方案2】:

      添加“名称”的简单解决方案不必难看:

      class Thing < ActiveRecord::Base
        has_one :foo, :class_name => "whazit", :conditions => { :name => "foo" }
        has_one :goo, :class_name => "whazit", :conditions => { :name => "goo" }
      end
      

      事实上,它与 STI 的工作方式非常相似,只是您不需要单独的类。

      您唯一需要注意的是在关联 whazit 时设置此名称。这可以很简单:

      def foo=(assoc)
        assos.name = 'foo'
        super(assoc)
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-07
        相关资源
        最近更新 更多