0

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.

Merry
  • 13
  • 3

1 Answers1

0

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.

Jeremy Evans
  • 11,959
  • 27
  • 26
  • not the solution unfortunately already tried a whole bunch of settings for the key including that. I dont think associations is what i want - i want to search pages by a custom uri held in the top level table, and have several classes of page to deal with specific behaviour and data. This screams Inheritance to me as there will be a large amount of repeated methods while necessary independence. I dont know why you wouldnt want to load the child from the parent in general anyhow, keeping to the Parent class would be the edge case. – Merry Nov 09 '16 at 15:55
  • Yeah you were actually correct, i could have sworn i had that set up to begin with (and then changed it while trying to fix stuff due to confusion) then when i tried changing it back the first time. Also it seems when load a Page it returns the child object by default anyway, which is what i wanted i just felt p.child was an easier way to explain it.its not working perfect yet but i hope i am no longer being stupid and i think i can fix it up... maybe. Unfortunately i am a little perplexed by the documentation on this plugin, it could maybe redone but i dont want to sound like an ass. ty. – Merry Nov 09 '16 at 16:09