Ruby on rails Rails:ActiveRecord::AssociationTypeMismatch:需要超类,获得子类
我正在尝试为我的应用程序创建一个类模型,它具有以下关键功能:Ruby on rails Rails:ActiveRecord::AssociationTypeMismatch:需要超类,获得子类,ruby-on-rails,inheritance,has-many,type-mismatch,Ruby On Rails,Inheritance,Has Many,Type Mismatch,我正在尝试为我的应用程序创建一个类模型,它具有以下关键功能: 继承(类Place充当Farm和Depot的超类)。我添加了用于实现此功能的 一个位置的关系(可以有多个位置) 我使用的是Rails 3.2.11和Ruby 1.9.3.p194 下面是我试图实现的一个粗略的类模型 您可以在位置模型中找到关系定义: class Place < ActiveRecord::Base acts_as_superclass attr_accessible :location, :name
- 继承(类
充当Place
和Farm
的超类)。我添加了用于实现此功能的Depot
- 一个
(可以有多个位置的关系
)位置
位置
模型中找到关系定义:
class Place < ActiveRecord::Base
acts_as_superclass
attr_accessible :location, :name, :subtype
has_many :place_connections, foreign_key: :place_a_id, dependent: :destroy
has_many :places, through: :place_connections, source: :place_b
has_many :reverse_place_connections, class_name: :PlaceConnection, \
foreign_key: :place_b_id, dependent: :destroy
end
但是,当我在Rails控制台中检查关系模型时,我会遇到以下情况:
> farm = Place.first
=> #<Farm place_id: 1, created_at: "2013-02-08 12:19:16", \
updated_at: "2013-02-08 12:19:16">
> depot = Place.last
=> #<Depot place_id: 6, created_at: "2013-02-08 12:19:44", \
updated_at: "2013-02-08 12:19:44">
> farm.places = [depot]
ActiveRecord::AssociationTypeMismatch: Place(#42600420) expected, \
got Depot(#42518820) ...
行动:
> farm = Place.first
=> #<Farm place_id: 1, created_at: "2013-02-08 12:19:16", \
updated_at: "2013-02-08 12:19:16">
> depot = Place.last
=> #<Depot place_id: 6, created_at: "2013-02-08 12:19:44", \
updated_at: "2013-02-08 12:19:44">
> farm.places = [depot.place]
ActiveRecord::AssociationTypeMismatch: Place(#42600420) expected, \
got Depot(#42518820) ...
> farm = Place.first
=> #<Farm place_id: 1, created_at: "2013-02-08 12:19:16", \
updated_at: "2013-02-08 12:19:16">
> depot = Place.last
=> #<Depot place_id: 6, created_at: "2013-02-08 12:19:44", \
updated_at: "2013-02-08 12:19:44">
> farm.places = [depot.place]
=> [#<Place id: 6, name: "Some depot", location: "Somewhere", subtype: "Depot", \
created_at: "2013-02-09 12:51:01", updated_at: "2013-02-09 12:51:01">]
行动:
> farm = Place.first
=> #<Farm place_id: 1, created_at: "2013-02-08 12:19:16", \
updated_at: "2013-02-08 12:19:16">
> depot = Place.last
=> #<Depot place_id: 6, created_at: "2013-02-08 12:19:44", \
updated_at: "2013-02-08 12:19:44">
> farm.places = [depot.place]
ActiveRecord::AssociationTypeMismatch: Place(#42600420) expected, \
got Depot(#42518820) ...
> farm = Place.first
=> #<Farm place_id: 1, created_at: "2013-02-08 12:19:16", \
updated_at: "2013-02-08 12:19:16">
> depot = Place.last
=> #<Depot place_id: 6, created_at: "2013-02-08 12:19:44", \
updated_at: "2013-02-08 12:19:44">
> farm.places = [depot.place]
=> [#<Place id: 6, name: "Some depot", location: "Somewhere", subtype: "Depot", \
created_at: "2013-02-09 12:51:01", updated_at: "2013-02-09 12:51:01">]
>farm=Place.first
=> #
>depot=最后一个地点
=> #
>farm.places=[depot.place]
=> [#]
请注意
我们意识到的另一件事是,一直以来都有可能扩展关系数组,如下所示:
farm.places在最后一行,尝试替换这个:
farm.places = [depot]
为此:
farm.places = [depot.place]
.place
方法应该返回place
的超类实例(这在技术上是PlaceConnection
所引用的),而不是Depot
的子类实例,听起来不错,尽管我不得不承认它会产生与以前相同的错误消息。嗯,如果输出depot.place.inspect
或depot.place.class
,会发生什么?它输出.inspect
的place
实例的散列以及.class
的place
属性。因此depot.place
听起来工作正常,按预期返回位置
实例。考虑到这一点,我无法想象为什么新的生产线不起作用。如果你试着这样做会发生什么<代码>farm.places=[Place.new]
?或者,如果这不起作用,请尝试将farm.places
替换为farm.depot.places
。我很想知道逻辑在哪里失败。赋值farm.places=[Place.new]
执行时没有错误。你的第二次测试对我来说毫无意义;farm
实例不公开名为depot
的访问者。这是打字错误吗?我猜你的意思是farm.place.places=[place.new]
,它也能成功执行。
farm.places = [depot.place]