ActiveRecord STI 删除无法正常工作

发布于 2024-09-26 20:57:30 字数 1793 浏览 3 评论 0原文

我想使用活动记录存储两个模型,但删除不能按预期工作。

  1. 评估具有 id、名称和描述
  2. ,SqlEvaluation 具有额外的两列:query_string 和数据库。

我想使用这两个表,eval_typ_id用于区分应该使用哪个子类:1表示SqlEvaluation。

create table eval (
eval_id int,
eval_name varchar,
eval_desc varchar,
eval_typ_id int 
);

create table sql_eval (
eval_id int
query_str varchar
database varchar
);

经过一番研究,我使用了以下代码,除了“delete”之外它运行良好,它没有删除 sql_eval 中的行。我无法弄清楚哪里出了问题?

require 'rubygems'
require 'active_record'
require 'logger'

ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(:adapter => "ibm_db",
                                        :username => "edwdq",
                                        :password => "edw%2dqr",
                                        :database => "EDWV2",
                                        :schema => "EDWDQ" )

class Eval < ActiveRecord::Base
  set_table_name "eval"
  set_primary_key :eval_id


  TYPE_MAP = { 1 => 'SqlEval' }

  class << self
    def find_sti_class(type)
      puts "#{type}"
      super(TYPE_MAP[type.to_i])
    end


    def sti_name
      TYPE_MAP.invert[self.name]
    end
  end

  set_inheritance_column :eval_typ_id
end

class SqlEval < Eval
  has_one :details, :class_name=>'SqlEvalDetails', :primary_key=>:eval_id, :foreign_key=>:eval_id, :include=>true, :dependent=>:delete
  default_scope :conditions => { :eval_typ_id => 1 }
end

class SqlEvalDetails < ActiveRecord::Base
  belongs_to :sql_eval, :class_name=>'SqlEval',
    :conditions => { :eval_type_id => 1 }
  set_table_name "sql_eval"
  set_primary_key :eval_id
end

se = SqlEval.find(:last)

require 'pp'

pp se
pp se.details

# Eval.delete(se.eval_id)
se.delete

I want to store two models using active record, but delete doesn't work as expected.

  1. Evaluation has id, name and description
  2. and SqlEvaluation has additional two columns of query_string and database.

I want to use those two tables, and eval_typ_id is used to distinguish which subclass should be used: 1 for SqlEvaluation.

create table eval (
eval_id int,
eval_name varchar,
eval_desc varchar,
eval_typ_id int 
);

create table sql_eval (
eval_id int
query_str varchar
database varchar
);

After some research, I used the following code, it works well except "delete", which didn't delete the row in sql_eval. I cannot figure out where is wrong?

require 'rubygems'
require 'active_record'
require 'logger'

ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(:adapter => "ibm_db",
                                        :username => "edwdq",
                                        :password => "edw%2dqr",
                                        :database => "EDWV2",
                                        :schema => "EDWDQ" )

class Eval < ActiveRecord::Base
  set_table_name "eval"
  set_primary_key :eval_id


  TYPE_MAP = { 1 => 'SqlEval' }

  class << self
    def find_sti_class(type)
      puts "#{type}"
      super(TYPE_MAP[type.to_i])
    end


    def sti_name
      TYPE_MAP.invert[self.name]
    end
  end

  set_inheritance_column :eval_typ_id
end

class SqlEval < Eval
  has_one :details, :class_name=>'SqlEvalDetails', :primary_key=>:eval_id, :foreign_key=>:eval_id, :include=>true, :dependent=>:delete
  default_scope :conditions => { :eval_typ_id => 1 }
end

class SqlEvalDetails < ActiveRecord::Base
  belongs_to :sql_eval, :class_name=>'SqlEval',
    :conditions => { :eval_type_id => 1 }
  set_table_name "sql_eval"
  set_primary_key :eval_id
end

se = SqlEval.find(:last)

require 'pp'

pp se
pp se.details

# Eval.delete(se.eval_id)
se.delete

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒带 2024-10-03 20:57:30

抱歉弄乱了代码。这是我第一次发帖。这是代码。

require 'rubygems'
require 'active_record'
require 'logger'

ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(:adapter => "ibm_db",
                                    :username => "edwdq",
                                    :password => "edw%2dqr",
                                    :database => "EDWV2",
                                    :schema => "EDWDQ" )

class Eval < ActiveRecord::Base
  set_table_name "eval"
  set_primary_key :eval_id


  TYPE_MAP = { 1 => 'SqlEval' }

  class << self
    def find_sti_class(type)
      puts "#{type}"
      super(TYPE_MAP[type.to_i])
    end

    def sti_name
      TYPE_MAP.invert[self.name]
    end
  end

  set_inheritance_column :eval_typ_id
end

class SqlEval < Eval
  has_one :details, :class_name=>'SqlEvalDetails', :primary_key=>:eval_id,     :foreign_key=>:eval_id, :include=>true, :dependent=>:delete
  default_scope :conditions => { :eval_typ_id => 1 }
end

class SqlEvalDetails < ActiveRecord::Base
  belongs_to :sql_eval, :class_name=>'SqlEval',
    :conditions => { :eval_type_id => 1 }
  set_table_name "sql_eval"
  set_primary_key :eval_id
end

se = SqlEval.find(:last)
e = Eval.where(:eval_id => 26)

require 'pp'

pp se
pp e
pp se.details

# Eval.delete(se.eval_id)
se.delete

Sorry for messing the code. It is first time to post for me. Here is the code.

require 'rubygems'
require 'active_record'
require 'logger'

ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(:adapter => "ibm_db",
                                    :username => "edwdq",
                                    :password => "edw%2dqr",
                                    :database => "EDWV2",
                                    :schema => "EDWDQ" )

class Eval < ActiveRecord::Base
  set_table_name "eval"
  set_primary_key :eval_id


  TYPE_MAP = { 1 => 'SqlEval' }

  class << self
    def find_sti_class(type)
      puts "#{type}"
      super(TYPE_MAP[type.to_i])
    end

    def sti_name
      TYPE_MAP.invert[self.name]
    end
  end

  set_inheritance_column :eval_typ_id
end

class SqlEval < Eval
  has_one :details, :class_name=>'SqlEvalDetails', :primary_key=>:eval_id,     :foreign_key=>:eval_id, :include=>true, :dependent=>:delete
  default_scope :conditions => { :eval_typ_id => 1 }
end

class SqlEvalDetails < ActiveRecord::Base
  belongs_to :sql_eval, :class_name=>'SqlEval',
    :conditions => { :eval_type_id => 1 }
  set_table_name "sql_eval"
  set_primary_key :eval_id
end

se = SqlEval.find(:last)
e = Eval.where(:eval_id => 26)

require 'pp'

pp se
pp e
pp se.details

# Eval.delete(se.eval_id)
se.delete
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文