用不同的 table 在 rails 6 中创建一个 ActiveRecord::Relation
Create an ActiveRecord::Relation in rails 6 with a different table
我正在尝试升级分区 gem 以便与 Rails 6.1 一起使用分区(我正在升级现有的 Rails 应用程序)。
我已经设法让一切正常工作,除了一部分:
在分区gem,为了查询分区table,
他们将与“正确的”arel_table(即我们要查询的实际分区 table)建立新关系。
以下语法适用于 Rails 3.2:
def self.from_partition(*partition_key_values)
table_alias_name = partition_table_alias_name(*partition_key_values)
return ActiveRecord::Relation.new(self, self.arel_table_from_key_values(partition_key_values, table_alias_name))
end
其中 self.arel_table_from_key_values(partition_key_values, table_alias_name)
是指向正确 table 名称(分区)的 Arel::Table
实例。
我已将语法更新为:
def self.from_partition(*partition_key_values)
table_alias_name = partition_table_alias_name(*partition_key_values)
return ActiveRecord::Relation.new(self, table: self.arel_table_from_key_values(partition_key_values, table_alias_name))
end
现在的问题是,当我尝试 运行 在 Rails 3.2 中有效的命令时,例如:
ItemLine.from_partition(11,1).find_by_code("1111")
我收到诸如 undefined method `find_by_code' for #<ActiveRecord::Relation
之类的错误
或者如果我尝试 运行
ItemLine.from_partition(11,1).last
我得到:
undefined local variable or method `implicit_order_column' for #<ActiveRecord::Relation
看来我创建关系的方式有误。
best/correct 在 Rails 6.1 中创建具有不同区域 table 的 ActiveRecord::Relation
的方法是什么?
谢谢!
在 ActiveRecord
、
的源代码中深入挖掘之后
我能够通过以下方式解决它:
def self.from_partition(*partition_key_values)
table_alias_name = partition_table_alias_name(*partition_key_values)
table = self.arel_table_from_key_values(partition_key_values, table_alias_name)
tm = ActiveRecord::TableMetadata.new(self,table)
predicate_builder = ActiveRecord::PredicateBuilder.new(tm)
return ActiveRecord::Relation.create(self, table: table, predicate_builder: predicate_builder)
end
我仍然需要 运行 对此解决方案进行一些测试,但到目前为止一切顺利! :-)
我正在尝试升级分区 gem 以便与 Rails 6.1 一起使用分区(我正在升级现有的 Rails 应用程序)。
我已经设法让一切正常工作,除了一部分:
在分区gem,为了查询分区table,
他们将与“正确的”arel_table(即我们要查询的实际分区 table)建立新关系。
以下语法适用于 Rails 3.2:
def self.from_partition(*partition_key_values)
table_alias_name = partition_table_alias_name(*partition_key_values)
return ActiveRecord::Relation.new(self, self.arel_table_from_key_values(partition_key_values, table_alias_name))
end
其中 self.arel_table_from_key_values(partition_key_values, table_alias_name)
是指向正确 table 名称(分区)的 Arel::Table
实例。
我已将语法更新为:
def self.from_partition(*partition_key_values)
table_alias_name = partition_table_alias_name(*partition_key_values)
return ActiveRecord::Relation.new(self, table: self.arel_table_from_key_values(partition_key_values, table_alias_name))
end
现在的问题是,当我尝试 运行 在 Rails 3.2 中有效的命令时,例如:
ItemLine.from_partition(11,1).find_by_code("1111")
我收到诸如 undefined method `find_by_code' for #<ActiveRecord::Relation
之类的错误
或者如果我尝试 运行
ItemLine.from_partition(11,1).last
我得到:
undefined local variable or method `implicit_order_column' for #<ActiveRecord::Relation
看来我创建关系的方式有误。
best/correct 在 Rails 6.1 中创建具有不同区域 table 的 ActiveRecord::Relation
的方法是什么?
谢谢!
在 ActiveRecord
、
的源代码中深入挖掘之后
我能够通过以下方式解决它:
def self.from_partition(*partition_key_values)
table_alias_name = partition_table_alias_name(*partition_key_values)
table = self.arel_table_from_key_values(partition_key_values, table_alias_name)
tm = ActiveRecord::TableMetadata.new(self,table)
predicate_builder = ActiveRecord::PredicateBuilder.new(tm)
return ActiveRecord::Relation.create(self, table: table, predicate_builder: predicate_builder)
end
我仍然需要 运行 对此解决方案进行一些测试,但到目前为止一切顺利! :-)