activerecord-typedstore icon indicating copy to clipboard operation
activerecord-typedstore copied to clipboard

Manage single table inheritance

Open bemueller opened this issue 9 years ago • 8 comments

Hi.

Nice framework that you wrote. Unfortunately there's an issue in a single table inheritance environment:

class Car < ActiveRecord::Base
  typed_store :properties do |t|
    t.integer :wheels, default: 4
  end
end

class Bus < Car
  typed_store :properties do |t|
    t.boolean :seat_belts, default: false
  end
end

When defining fields for both base and child classes, the the defined columns aren't merged. Hence objects of Bus have getters/setters for :wheels but the type is not checked and the default value is not set automatically.

bemueller avatar Feb 15 '16 12:02 bemueller

Hum, indeed. We miss proper inheritance.

byroot avatar Feb 17 '16 18:02 byroot

Just checking. This is still a problem right? No support for inheritance?

CyborgMaster avatar Oct 26 '16 22:10 CyborgMaster

Well, you can repeat the parent class definition and it will work. But no it won't merge automatically.

A PR would be welcome though.

byroot avatar Oct 26 '16 22:10 byroot

K. Thanks for the clarification. I think for now that I will just repeat the declaration. Maybe I'll find time in the future for a PR.

CyborgMaster avatar Oct 26 '16 22:10 CyborgMaster

Now that there is a PR for it, any chance of getting it merged?

CyborgMaster avatar Jul 27 '18 21:07 CyborgMaster

The PR never passed CI.

byroot avatar Jul 28 '18 23:07 byroot

@byroot fair enough.

CyborgMaster avatar Jul 30 '18 15:07 CyborgMaster

This seems to no longer be an issue? I was not aware it could be, tried it, worked.

class SurveyQuestion < ApplicationRecord
  typed_store :settings, coder: ActiveRecord::TypedStore::IdentityCoder do |s|
    s.string :internal_uid
  end
end
class SurveyQuestions::Text < SurveyQuestion
  typed_store :settings, coder: ActiveRecord::TypedStore::IdentityCoder do |s|
    s.string :input_type, default: "text"
  end
end

Then

text_question = SurveyQuestions::Text.new(internal_uid: 1, input_type: "text")
puts JSON.pretty_generate text_question.attributes # { "id": null, "settings": {"input_type": "text", "internal_uid": 1 } }
puts text_question.internal_uid # 1
puts text_question.input_type # text

This may still be an issue with the default coder and not the identity one? Didn't get to check that.

dimvic avatar Mar 30 '23 17:03 dimvic