I have an ActiveHash class that I'd like to use in a polymorphic association:
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
When setting the polymorphic association, it persists to the database setting the polymorphic type to ActiveHash::Base
. When accessing it later, I receive an undefined method 'arel_table'
error.
person = Person.create
person.placeable = Country.last
person.save
person
=> #<Person id: 1, placeable_id: 2, placeable_type: "ActiveHash::Base">
person.placeable
NoMethodError: undefined method `arel_table' for ActiveHash::Base:Class
from (irb):2
I've also tried setting the polymorphic type to Country
, but I receive the same error for the Country class.
A non-polymorphic belongs_to
seems to work fine.
See Doc: https://github.com/zilkey/active_hash/issues/151
**
ActiveHash does not support arel behaviour.
**
Arel is a SQL AST (Abstract Syntax Tree-like) manager for Ruby. It allows us to write complex SQL queries in a semantic, reusable fashion. Arel is "framework framework"; it's designed to optimize object and collection modeling over database compatibility. Active Record is built on top of Arel.
see Doc: https://www.rubydoc.info/gems/honkster-active_hash/0.7.3
Even in this official doc, it doesn't explain anywhere like this:
has_many :people, as: :placeable
Just explain
belongs_to :placeable, polymorphic: true