Rails fixtures使用带有CLOB字段的empty_clob(),但没有别的

时间:2014-11-20 01:58:51

标签: ruby-on-rails oracle rails-activerecord

自从我上次使用它们以来它们得到了很大的改进后,我回到了Rails灯具。

#models.yml
one:
  id: 1
  clob_field: "My Text"

当模型夹具加载到DB中时 - 我可以看到clob文本(My Text)被替换为empty_clob()调用(在insert语句中)

根据我的理解,Oracle增强适配器应该创建另一个适当设置clob_field的更新语句 - 但这不会被执行(并且值保持空白)。

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

我跟踪了夹具加载,发现这是由于混合了指定一个schema_name和table_name(self.table_name = "SCHEMA_OWNER.TABLE_NAME")以及使用大写的TABLE_NAMES。

我通过覆盖insert_fixture方法(在oracle-enhanced适配器中)正确操作table_name来解决这个问题。

现在正确调用write_lobs

更新

以下是@ jeff-k

所要求的变更

#config / initializers / oracle_enhanced_adapter.rb

  ...
  ActiveSupport.on_load(:active_record) do
  ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.class_eval do


    # Overriding this method to account for including the schema_name in the table name
    # which is implemented to work around another limitation of having the schema_owner different
    # than the connected user
    #
    # Inserts the given fixture into the table. Overridden to properly handle lobs.
    def insert_fixture(fixture, table_name) #:nodoc:
      super

      if table_name =~ /\./i
        table_name = table_name.downcase.split('.')[1]
      end

      if ActiveRecord::Base.pluralize_table_names
        klass = table_name.to_s.singularize.camelize
      else
        klass = table_name.to_s.camelize
      end

      klass = klass.constantize rescue nil
      if klass.respond_to?(:ancestors) && klass.ancestors.include?(ActiveRecord::Base)
        write_lobs(table_name, klass, fixture, klass.lob_columns)
      end
    end
  end
end