开发者社区> 问答> 正文

Polymorphic Association不使用ActiveHash

我有一个ActiveHash类,我想在多态关联中使用它:

class Person < ActiveRecord::Base
extend ActiveHash::Associations::ActiveRecordExtensions

belongs_to :placeable, polymorphic: true
end

class Country < ActiveHash::Base
include ActiveHash::Associations

has_many :people, as: :placeable

self.data = [

{ id: 1, name: 'US' },
{ id: 2, name: 'Canada' }

]
end

设置多态关联时,它会持久保存到将多态类型设置为的数据库ActiveHash::Base。稍后访问时,我收到undefined method 'arel_table'错误。

person = Person.create
person.placeable = Country.last
person.save
person
=> #

person.placeable
NoMethodError: undefined method `arel_table' for ActiveHash::Base:Class

from (irb):2

我也尝试将多态类型设置为Country,但我收到Country类的相同错误。

非多态belongs_to似乎工作得很好。

展开
收起
小六码奴 2019-04-10 13:46:20 1719 0
1 条回答
写回答
取消 提交回答
  • ActiveHash不支持arel行为。

    **

    Arel是Ruby的SQL AST(类似抽象语法树)管理器。它允许我们以语义,可重用的方式编写复杂的SQL查询。Arel是“框架框架”; 它旨在优化对象和集合建模而不是数据库兼容性。Active Record建立在Arel之上。

    见Doc:https://www.rubydoc.info/gems/honkster-active_hash/0.7.3

    即使在这个官方文档中,它也没有解释如下:

    has_many :people, as: :placeable
    请解释一下

    belongs_to :placeable, polymorphic: true

    2019-07-17 23:33:07
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载