Thanks! That did the trick as far as getting typecasting and actually 
inserting bio when creating an instance of the subclass. However, lazy 
loading bio when eager loading the subclass didn't seem to work. I should 
have specified that in my original post but that's really the outcome I 
need: lazy load bio when eager loading the subclass.

Your last post made me try just manually setting @db_schema after adding 
lazy_attributes in the subclass and it seemed to work as I hoped.

class Employee < Sequel::Model
  plugin :class_table_inheritance, key: :kind
end

class Manager < Employee
  old_schema = @db_schema.dup
  plugin :lazy_attributes, :bio
  @db_schema = old_schema
end


Let me know if you think there's any gotchas you can think of with that 
approach. Otherwise, thank you for your help!






On Thursday, October 21, 2021 at 11:44:32 AM UTC-7 Jeremy Evans wrote:

> On Tue, Oct 19, 2021 at 4:26 PM s.brimd...@gmail.com <s.brimd...@gmail.com> 
> wrote:
>
>> Thanks for getting back to me!
>>
>> Assuming you meant to use lazy attributes in both the parent class and 
>> the subclass, it semi-worked in that I no longer see the missing column 
>> errors on the subclass. However, we still don't have typecasting and it 
>> seems I can't update the lazy loaded column from the subclass.
>>
>> Here is my setup using the same DB schema as before
>>
>> class Employee < Sequel::Model
>>   plugin :lazy_attributes, :bio
>>   plugin :class_table_inheritance, key: :kind
>> end
>>
>> class Manager < Employee
>>   old_from = dataset.opts[:from][0]
>>   set_dataset 
>> dataset.from(old_from.expression.select_append{employees[:bio]}.as(old_from.alias))
>>   lazy_attributes :bio
>> end
>>
>> *Employee works as expected with typecasting and setting the lazy loaded 
>> attribute*
>>
>> employee = Employee.create active: 0, bio: "Superclass bio"
>> puts "employee's biography: #{employee.bio}"
>>
>>
>>
>> SQL
>>
>> INSERT INTO "employees" ("active", "bio", "kind") VALUES (false, 
>> 'Superclass bio', 'Employee') RETURNING "id"
>> SELECT "employees"."id", "employees"."kind", "employees"."active" FROM 
>> "employees" WHERE ("id" = 1) LIMIT 1
>> SELECT "employees"."bio" FROM "employees" WHERE ("id" = 1) LIMIT 1
>>
>> *Manager still doesn't work quite right. The lazy loading works but bio 
>> is never inserted*
>>
>> manager = Manager.create active: false, bio: "Manager bio"
>> manager.reload
>> puts %Q{manager's biography is "Manager bio": #{manager.bio == "Manager 
>> bio"}}
>>
>> SQL
>>
>> INSERT INTO "employees" ("kind", "active") VALUES ('Manager', false) 
>> RETURNING *
>> INSERT INTO "managers" ("id") VALUES (2) RETURNING *
>> SELECT "employees"."id", "employees"."kind", "employees"."active" FROM 
>> (SELECT "employees"."id", "employees"."kind", "employees"."active", 
>> "employees"."bio" FROM "employees" INNER JOIN "managers" ON 
>> ("managers"."id" = "employees"."id")) AS "employees" WHERE ("id" = 2) LIMIT 
>> 1
>> SELECT "employees"."bio" FROM (SELECT "employees"."id", 
>> "employees"."kind", "employees"."active", "employees"."bio" FROM 
>> "employees" INNER JOIN "managers" ON ("managers"."id" = "employees"."id")) 
>> AS "employees" WHERE ("id" = 2) LIMIT 1
>>
>>
>> *Typecasting is also not working for the subclass*
>>
>> Manager.create active: 0
>>
>> ERROR -- : PG::DatatypeMismatch: ERROR:  column "active" is of type 
>> boolean but expression is of type integer
>> LINE 1: ..."employees" ("kind", "active") VALUES ('Manager', 0) 
>> RETURNI...                                                          ^
>> HINT:  You will need to rewrite or cast the expression.: INSERT INTO 
>> "employees" ("kind", "active") VALUES ('Manager', 0) RETURNING *
>> manager = Manager.create active: false, bio: "Manager bio"
>>
>>
>> I suspect it's because the schema and columns for Manager are missing the 
>> bio column.
>>
>> irb(main):001:0> Manager.db_schema
>> => {:id=>{}, :kind=>{}, :active=>{}}
>> irb(main):002:0> Manager.columns
>> => [:id, :kind, :active]
>>
>
> Sorry for the delay in responding.  I've tested the following and it 
> should handle your case:
>
>  class Employee < Sequel::Model
>   plugin :lazy_attributes, :bio
>   plugin :class_table_inheritance, key: :kind
>   cti_table_columns << :bio
>
> end
>
> class Manager < Employee
>   old_from = dataset.opts[:from][0]
>   @dataset = 
> dataset.from(old_from.expression.select_append{employees[:bio]}.as(old_from.alias))
> end
>
> If you continue to run into problems, please let me know.
>
> 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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/70587116-e600-43af-a8e9-16aaa01a3777n%40googlegroups.com.

Reply via email to