ruby-on-railsrubyinheritancesequelclass-table-inheritance

Sequel class table inheritance


I am hoping to start a project using Multiple/Class table inheritance in Rails. To do this i am planning to use Sequel instead of ActiveRecord. The Docs can be found here - Sequel - class table inheritance

I have the Plugin working to an extent, I can call child.first and it will give me the first of the child classes, which is not the first of the parent class, and it includes the data from the child table. The problem is going the other way...

So if i load a db row via the parent class, which happens to be a child2 class

p = Parent.first <Parent @values = {kind = "Child2"}>

and then try to get the child data by using the refresh command listed in the docs it SHOULD give me

p.refresh <Parent @values = {kind = "Child2", child2data = "data from child 2 table"}>

but it doesnt, it just returns the unaltered parent

I have included a model map in my parent class

 plugin :class_table_inheritance,:key=>"kind", :model_map=>{"Child1"=>:Child1,"Child2"=>:Child2}

And i have played about with different values and other nonsense it to try and get it to work, but no success.

What i ideally want is to be able to find the parent and call a method to instance the child class so

p = Parent.first
c = p.child
c <Child2 @values = {kind: "Child2", Child2data: "data from child2 table">

I feel like i can probably hack/add a method to my parent class to do this but it just feels quite simple at this point that i shouldnt have to, also the fact that the refresh method doesnt work worries me and id like that to be resolved

Any one have an answer? ty for reading

p.s. i am using rails 5, the rails-sequel gem, and am working in a project that is more or less blank, i just want to have a decent CTI setup because to me it seems so much more elegant than Polymorphic associations.


Solution

  • Use :key=>:kind in the plugin call, that should fix your issues. If you want a p.child method, use associations instead of the class_table_inheritance plugin.