Polymorphic hacker models

Despite the fact that I know that, at least as far as I know, this is not a standard way of creating associations with ActiveRecord. I am looking for help to implement a “hibernating" polymorphic model.

For example, consider the following basic model:

# Content only has one property named path
class Content < ActiveRecord::Base
  self.abstract_class = true
end

And specific content:

# Audio only has one property on it own (duration).
# However, it should also inherit Content property (path)
class Audio < Content
end

Now something relatively interesting happens when using ActiveRecord, or rather Rails 3 beta 3 ActiveRecord. If you set abstract_class to false in the Content model and do the following:

Audio.create!(:path => '/dev/null')

It looks like sleep mode. That is, a content recording is created with identifier 1, and an audio recording is also created with identifier ID = 1.

# 1 , , , , , _, , , .

, # 2 , abstract_class Audio. , , Audio Audio.

, , , ActiveRecord + , Audio. , , Audio :

audio = Audio.new #=> <Audio id: nil, duration: nil, path: nil, created_at: nil, updated_at: nil> 

[], audio.path = '/dev/null' , ActiveRecord , path , . , , ActiveRecord, .

, , , ?

, , , . ?

PS: , . , .

, DBA

+3
1

, , , , , , .

ActiveRecord :

  • , , . #path Content attr_accessor, , , "".

  • , .

, , ActiveRecord . 1:1 .

+2

Source: https://habr.com/ru/post/1745165/


All Articles