0

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

  1. 评估有 id、name 和 description
  2. 并且 SqlEvaluation 有另外两列 query_string 和 database。

我想用那两个表,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
4

1 回答 1

0

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

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
于 2010-10-12T23:07:27.167 回答