byroot / activerecord-typedstore

ActiveRecord::Store but with type definition
MIT License
436 stars 57 forks source link

Manage single table inheritance #36

Open bemueller opened 8 years ago

bemueller commented 8 years ago

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.

byroot commented 8 years ago

Hum, indeed. We miss proper inheritance.

CyborgMaster commented 7 years ago

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

byroot commented 7 years ago

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.

CyborgMaster commented 7 years ago

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 commented 5 years ago

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

byroot commented 5 years ago

The PR never passed CI.

CyborgMaster commented 5 years ago

@byroot fair enough.

dimvic commented 1 year ago

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.