Ruby on rails 3 多态性:在Rails3.1插件中有很多,:通过as模块
我到处寻找指向这个的指针,但找不到。基本上,当其他人在一个:has_许多,:through方式中创建多态关系时,我想做他们想做的事情,但我想在一个模块中做。我一直被卡住,我想我一定忽略了一些简单的事情 也就是说:Ruby on rails 3 多态性:在Rails3.1插件中有很多,:通过as模块,ruby-on-rails-3,rails-models,has-many-polymorphs,Ruby On Rails 3,Rails Models,Has Many Polymorphs,我到处寻找指向这个的指针,但找不到。基本上,当其他人在一个:has_许多,:through方式中创建多态关系时,我想做他们想做的事情,但我想在一个模块中做。我一直被卡住,我想我一定忽略了一些简单的事情 也就是说: module ActsPermissive module PermissiveUser def self.included(base) base.extend ClassMethods end module ClassMethods
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
当然,我们的想法是,无论加载什么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
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?
Failure/Error: @user.circles.should be_an_instance_of Array
NameError:
uninitialized constant User::CircleOwner
我尝试过:在has\u many:owneables行中使用:class\u name=>“ActsPermissive::Permissive Circle”
,但失败的原因是:
Failure/Error: @user.circles.should be_an_instance_of Array
NameError: uninitialized constant User::Ownable
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?
Failure/Error: @user.circles.should be_an_instance_of Array
NameError:
uninitialized constant User::CircleOwner
这似乎表明,使用非多态性through进行操作是必要的。因此,我添加了一个circle_owner类,类似于:
但在以下情况下仍然失败:
Failure/Error: @user.circles.should be_an_instance_of Array
NameError: uninitialized constant User::Ownable
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?
Failure/Error: @user.circles.should be_an_instance_of Array
NameError:
uninitialized constant User::CircleOwner
这让我们回到了开始
事实证明,将:class\u name添加到两个:has\u的许多定义中实际上是可行的(有人对此发表了评论,但他们删除了自己的评论)。它在我的非简化程序中不起作用,因为我的程序中有其他东西导致了级联错误,这似乎是:has_许多定义的局部错误 短篇故事:这是一个很多麻烦的事情,其实不是一个问题。出血