繁体   English   中英

:Has_many,:through作为Rails 3.1插件中的模块

[英]Polymorphic :has_many, :through as module in Rails 3.1 plugin

我到处都在寻找指向这个的指针,但是找不到一个。 基本上,我想做其他人在:has_many,:through方式中创建多态关系时想要做的事情,但是我想在模块中做。 我一直陷于困境,并认为我必须忽略一些简单的事情。

以机智:

module ActsPermissive
  module PermissiveUser
    def self.included(base)
      base.extend ClassMethods
    end
    module ClassMethods
      def acts_permissive
        has_many  :ownables
        has_many  :owned_circles, :through => :ownables
      end
    end
  end

  class PermissiveCircle < ActiveRecord::Base
    belongs_to    :ownable, :polymorphic => true
  end
end

使用如下所示的迁移:

create_table :permissive_circles do |t|
  t.string :ownable_type
  t.integer :ownable_id
  t.timestamps
end

当然,这个想法是,无论负载acts_permissive如何,都将能够拥有其拥有的圆的列表。

对于简单的测试,我有

it "should have a list of circles" do
  user = Factory :user
  user.owned_circles.should be_an_instance_of Array
end

失败与:

Failure/Error: @user.circles.should be_an_instance_of Array
     NameError: uninitialized constant User::Ownable

我试过了:在has_many:ownables行上使用:class_name => 'ActsPermissive::PermissiveCircle' ,但失败了:

Failure/Error: @user.circles.should be_an_instance_of Array
     ActiveRecord::HasManyThroughSourceAssociationNotFoundError:
      Could not find the source association(s) :owned_circle or 
      :owned_circles in model ActsPermissive::PermissiveCircle. 
      Try 'has_many :owned_circles, :through => :ownables, 
      :source => <name>'. Is it one of :ownable?

在遵循建议并设置:source => :ownable失败

Failure/Error: @user.circles.should be_an_instance_of Array
     ActiveRecord::HasManyThroughAssociationPolymorphicSourceError:
       Cannot have a has_many :through association 'User#owned_circles' 
       on the polymorphic object 'Ownable#ownable'

这似乎表明使用非多态贯通进行处理是必要的。 所以我添加了一个类似于这里的设置的circle_owner类:

module ActsPermissive
  class CircleOwner < ActiveRecord::Base
    belongs_to :permissive_circle
    belongs_to :ownable, :polymorphic => true
  end

  module PermissiveUser
    def self.included(base)
      base.extend ClassMethods
    end
    module ClassMethods
      def acts_permissive
        has_many  :circle_owners, :as => :ownable
        has_many  :circles, :through => :circle_owners, 
                  :source => :ownable, 
                  :class_name => 'ActsPermissive::PermissiveCircle'
      end
    end

  class PermissiveCircle < ActiveRecord::Base
    has_many :circle_owners
  end
end

随着迁移:

create_table :permissive_circles do |t|
  t.string :name
  t.string :guid

  t.timestamps
end

create_table :circle_owner do |t|
  t.string  :ownable_type
  t.string  :ownable_id
  t.integer :permissive_circle_id
end

仍然失败:

Failure/Error: @user.circles.should be_an_instance_of Array
     NameError:
       uninitialized constant User::CircleOwner

这使我们回到了开始。

  1. 如何在模块上执行似乎很常见的多态:has_many,:through?
  2. 或者,是否有一种好方法允许任意对象以类似于模块的方式收集对象?

事实证明,将:class_name添加到两个:has_many定义中实际上可以工作(有人对此进行了评论,但他们删除了他们的评论)。 它在我的非简化程序中不起作用,原因是程序中的其他原因导致了SEEMED在:has_many定义本地的级联错误。

简短的故事:对于实际上不是问题的事情,这很麻烦。 布莱奇

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM