On Thu, Sep 16, 2021 at 10:48 AM 'Andrew Wei' via sequel-talk <
[email protected]> wrote:

> Hello,
>
> I was working my way through implementing the single table inheritance
> plugin
> <https://sequel.jeremyevans.net/rdoc-plugins/classes/Sequel/Plugins/SingleTableInheritance.html>
>  and
> I think I found a few typos in the documentation.
>
> I found that using *key_map:* instead of *model_map:* properly maps the
> class to the type column. (I'm not sure how it's supposed to be
> implemented, but *model_map:* sounds like it should be the appropriate
> option name for this.)
>
> Additionally, I found that I had to swap the direction of the map hashes
> for the plugin to work correctly. For example, the map should look like
> this instead: *{:Staff => 1, :Manager => 2}*
>
> Using the example in the docs, I found that the following changes need to
> be made to get it working correctly:
>
> Doc example:
>
> *Employee.plugin :single_table_inheritance, :type, model_map: {1=>:Staff,
> 2=>:Manager}*
>
> How I got it to work:
> *Employee.plugin :single_table_inheritance, :type, key_map: {:Staff =>
> 1, :Manager => 2}*
>
> I just wanted to share my findings and confirm that I'm using the plugin
> correctly.
>

model_map is used to map the value of the :type column (the key column) to
the appropriate model class name to use.  key_map is used to map the model
class name to the appropriate key column value(s).  You want both options.
With just key_map and no model_map, you'll probably end up with an error
when you retrieve a record:

DB.create_table(:staff) do
  primary_key :id
  Integer :type
end

class Staff < Sequel::Model(:staff)
  plugin :single_table_inheritance, :type, key_map: {:Staff=>1,
:Manager=>2}#, model_map: {1=>:Staff, 2=>:Manager}
end

class Manager < Staff
end

Staff.create
Manager.create

Staff.all
# Invalid class type used: 1 (Sequel::Error)

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CADGZSSeYP-5qT7Eopvjp1wa%3D%3Dq4rGwjCzMp2dKPe8f6imCBASA%40mail.gmail.com.

Reply via email to