Ruby Sequel STI model_map或key_map问题与DB查询

时间:2018-04-16 08:04:53

标签: ruby postgresql module sequel single-table-inheritance

我有一个带有'parent'类的模块, Trait 及其'children'继承的类, Text Image ,正在使用在模块中使用Postgresql的Ruby Sequel gem, Tank

module Tank; end

module Tank
  class Trait < Sequel::Model
    plugin :single_table_inheritance, :type
  end
end

module Tank
  class Image < Trait; end
end

module Tank
  class Text < Trait; end
end

在控制台中,我尝试在特征上调用#destroy。这会导致错误,因为Sequel会尝试使用#type Tank :: Text 查找 Trait ,如下所示。

> Trait[439555].destroy
Sequel::NoExistingObject: Attempt to delete object did not result in
a single row modification (Rows Deleted: 0, SQL: DELETE FROM "traits" 
WHERE (("traits"."type" IN ('Tank::Text')) AND ("id" = 439555)))

我尝试过使用一些高级选项; #model_map,#key_map和#key_chooser,如续集文档中所示:http://sequel.jeremyevans.net/rdoc-plugins/classes/Sequel/Plugins/SingleTableInheritance.html

不可否认,我对lambdas和procs并不熟悉,但这是我的尝试:

module HoldingTank
  class Trait < Sequel::Model
    plugin :single_table_inheritance, :type,
    model_map: { 'Text'=>:Text, 'Image'=>:Image }
  end
end

我尝试在不起作用的情况下添加以下内容:

key_chooser: lambda {|i| i.model.sti_key_map[i.model.to_s.split('::').last].first}

两次尝试都有类似的结果,现在它正在搜索1 = 0的行(显然出现问题):

> Trait[439555].destroy
Sequel::NoExistingObject: Attempt to delete object did not result in
a single row modification (Rows Deleted: 0, SQL: DELETE FROM "traits"
WHERE ((1 = 0) AND ("id" = 439555)))

为了它的价值,在我添加STI之前, Trait 类已经完全作为 Text 条目存在。我更新了所有当前的 Trait 实例,以便在控制台中确认了#type ='Text'。我也得到了这些结果,我不知道他们是否应该被期待,因为我是Sequel和STI的新手:

> Text.count
=> 0
> Trait.where(type: 'Text').count
=> 931589

提前感谢您的任何帮助。

0 个答案:

没有答案