TalentBox / sequel-rails

A gem for using Sequel with Rails 5.x, 6.x, 7.x
http://talentbox.github.io/sequel-rails/
MIT License
325 stars 81 forks source link

Class Table Inheritance when Models are within Modules #112

Open nlgcat opened 8 years ago

nlgcat commented 8 years ago

I have managed to set up CTI as per the instructions on this repo, along with the separate rails-generators. I am now trying to create classes within their own Module, such as you can do with ActiveRecord.

When I try the below however I get an error on the child although the parent works:

Output from running rake db:seed (a call to Parent.create, then a call to a separate Child.create)

<Test::Parent @values={:id=>1, :name=>"The Parent", :content_type=>"Test::Parent"}>

NoMethodError: undefined method `Parent' for Test:Module

The various code snippets are shown below. Is it possible to do this with sequel-rails or is it currently unsupported? The Child class does not seem to be able to find the parent class when declared in this way.

Many thanks.

Code Snippets:

create_test_parents.rb

Sequel.migration do 
  change do
    create_table :test_parents do
      primary_key :id
      String :name
      String :content_type
    end
  end
end

create_test_children.rb

Sequel.migration do 
  change do
    create_table :test_children do
      primary_key :id
      Integer :age
    end
  end
end

app/models/test/parent.rb

class Test::Parent < Sequel::Model(:test_parents)
  plugin :class_table_inheritance, key: :content_type
end

app/model/test/child.rb

class Test::Child < Test::Parent(:test_children)

end

db/seeds.rb

require 'pp'
parent = Test::Parent.create(name: "The Parent")
pp parent
child = Test::Child.create(name: "The Child", age: 3)
zlobz commented 8 years ago

change Test::Parent(:test_children) to Test::Parent

http://sequel.jeremyevans.net/rdoc-plugins/classes/Sequel/Plugins/ClassTableInheritance.html

# Specifying the tables with a :table_map hash
Employee.plugin :class_table_inheritance,
  :table_map=>{:Employee  => :employees,
               :Staff     => :staff,
               :Cook      => :staff,
               :Manager   => :managers,
               :Executive => :executives,
               :CEO       => :executives }
nlgcat commented 8 years ago

Many thanks for the reply.

edit: Never mind the further question if you had seen it, I had a typo