Ruby on rails 如何在更新的rails3.1应用程序中保留旧的activerecord 2.3 associations.rb
我有一些继承的代码: 我有一个Rails 2.3应用程序,我已经更新到Rails 3.1 主要问题: 在我继承的代码中,有一个文件使用了这些不推荐使用的方法Ruby on rails 如何在更新的rails3.1应用程序中保留旧的activerecord 2.3 associations.rb,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我有一些继承的代码: 我有一个Rails 2.3应用程序,我已经更新到Rails 3.1 主要问题: 在我继承的代码中,有一个文件使用了这些不推荐使用的方法 alias_method_chain :association_join, :fix 及 关联联接和构造联接已被弃用,我不知道应该用什么替换它们。有人知道我可以把它们重构成什么吗?我似乎在网上找不到任何信息。或者有没有一个gem可以解决这个弃用问题 在应用程序内部有一个rails 2.3“修复”文件用于关联,现在在rails 3.1更新下
alias_method_chain :association_join, :fix
及
关联联接和构造联接已被弃用,我不知道应该用什么替换它们。有人知道我可以把它们重构成什么吗?我似乎在网上找不到任何信息。或者有没有一个gem可以解决这个弃用问题
在应用程序内部有一个rails 2.3“修复”文件用于关联,现在在rails 3.1更新下失败了…为了清晰起见,下面列出了该文件的一般结构
“修复”文件
。
.
下面列出了感兴趣的用户的完整文件
module ActiveRecord
module Associations
module ClassMethods
class JoinDependency
class JoinAssociation
def association_join_with_fix
connection = reflection.active_record.connection
join = case reflection.macro
when :has_and_belongs_to_many
" #{join_type} %s ON %s.%s = %s.%s " % [
table_alias_for(options[:join_table], aliased_join_table_name),
connection.quote_table_name(aliased_join_table_name),
options[:foreign_key] || reflection.active_record.to_s.foreign_key,
connection.quote_table_name(parent.aliased_table_name),
reflection.active_record.primary_key] +
" #{join_type} %s ON %s.%s = %s.%s " % [
table_name_and_alias,
connection.quote_table_name(aliased_table_name),
klass.primary_key,
connection.quote_table_name(aliased_join_table_name),
options[:association_foreign_key] || klass.to_s.foreign_key
]
when :has_many, :has_one
case
when reflection.options[:through]
through_conditions = through_reflection.options[:conditions] ? "AND #{interpolate_sql(sanitize_sql(through_reflection.options[:conditions]))}" : ''
jt_foreign_key = jt_as_extra = jt_source_extra = jt_sti_extra = nil
first_key = second_key = as_extra = nil
if through_reflection.options[:as] # has_many :through against a polymorphic join
jt_foreign_key = through_reflection.options[:as].to_s + '_id'
jt_as_extra = " AND %s.%s = %s" % [
connection.quote_table_name(aliased_join_table_name),
connection.quote_column_name(through_reflection.options[:as].to_s + '_type'),
klass.quote_value(parent.active_record.base_class.name)
]
else
jt_foreign_key = through_reflection.primary_key_name
end
case source_reflection.macro
when :has_many
if source_reflection.options[:as]
first_key = "#{source_reflection.options[:as]}_id"
second_key = options[:foreign_key] || primary_key
as_extra = " AND %s.%s = %s" % [
connection.quote_table_name(aliased_table_name),
connection.quote_column_name("#{source_reflection.options[:as]}_type"),
klass.quote_value(source_reflection.active_record.base_class.name)
]
else
first_key = source_reflection.association_foreign_key # through_reflection.klass.base_class.to_s.foreign_key
second_key = source_reflection.primary_key_name # options[:foreign_key] || primary_key
end
unless through_reflection.klass.descends_from_active_record?
jt_sti_extra = " AND %s.%s = %s" % [
connection.quote_table_name(aliased_join_table_name),
connection.quote_column_name(through_reflection.active_record.inheritance_column),
through_reflection.klass.quote_value(through_reflection.klass.sti_name)]
end
when :belongs_to
first_key = primary_key
if reflection.options[:source_type]
second_key = source_reflection.association_foreign_key
jt_source_extra = " AND %s.%s = %s" % [
connection.quote_table_name(aliased_join_table_name),
connection.quote_column_name(reflection.source_reflection.options[:foreign_type]),
klass.quote_value(reflection.options[:source_type])
]
else
second_key = source_reflection.primary_key_name
end
end
" #{join_type} %s ON (%s.%s = %s.%s%s%s%s) " % [
table_alias_for(through_reflection.klass.table_name, aliased_join_table_name),
connection.quote_table_name(aliased_join_table_name),
connection.quote_column_name(parent.primary_key),
connection.quote_table_name(parent.aliased_table_name),
connection.quote_column_name(jt_foreign_key),
jt_as_extra, jt_source_extra, jt_sti_extra
] +
" #{join_type} %s ON (%s.%s = %s.%s%s) " % [
table_name_and_alias,
connection.quote_table_name(aliased_table_name),
connection.quote_column_name(first_key),
connection.quote_table_name(aliased_join_table_name),
connection.quote_column_name(second_key),
as_extra
]
when reflection.options[:as] && [:has_many, :has_one].include?(reflection.macro)
" #{join_type} %s ON %s.%s = %s.%s AND %s.%s = %s" % [
table_name_and_alias,
connection.quote_table_name(aliased_table_name),
"#{reflection.options[:as]}_id",
connection.quote_table_name(parent.aliased_table_name),
parent.primary_key,
connection.quote_table_name(aliased_table_name),
"#{reflection.options[:as]}_type",
klass.quote_value(parent.active_record.base_class.name)
]
else
foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
" #{join_type} %s ON %s.%s = %s.%s " % [
table_name_and_alias,
aliased_table_name,
foreign_key,
parent.aliased_table_name,
reflection.options[:primary_key] || parent.primary_key
]
end
when :belongs_to
" #{join_type} %s ON %s.%s = %s.%s " % [
table_name_and_alias,
connection.quote_table_name(aliased_table_name),
reflection.klass.primary_key,
connection.quote_table_name(parent.aliased_table_name),
options[:foreign_key] || reflection.primary_key_name
]
else
""
end || ''
join << %(AND %s) % [
klass.send(:type_condition, aliased_table_name)] unless klass.descends_from_active_record?
[through_reflection, reflection].each do |ref|
join << "AND #{interpolate_sql(sanitize_sql(ref.options[:conditions], aliased_table_name))} " if ref && ref.options[:conditions]
end
join
end
alias_method_chain :association_join, :fix
end
end
end
end
end
module ActiveRecord
module Associations
class HasManyThroughAssociation
def construct_joins_with_fix(custom_joins = nil)
polymorphic_join = nil
if @reflection.source_reflection.macro == :belongs_to
reflection_primary_key = @reflection.klass.primary_key
source_primary_key = @reflection.source_reflection.primary_key_name
if @reflection.options[:source_type]
polymorphic_join = "AND %s.%s = %s" % [
@reflection.through_reflection.quoted_table_name, "#{@reflection.source_reflection.options[:foreign_type]}",
@owner.class.quote_value(@reflection.options[:source_type])
]
end
else
reflection_primary_key = @reflection.source_reflection.primary_key_name
source_primary_key = @reflection.source_reflection.association_foreign_key
if @reflection.source_reflection.options[:as]
polymorphic_join = "AND %s.%s = %s" % [
@reflection.quoted_table_name, "#{@reflection.source_reflection.options[:as]}_type",
@owner.class.quote_value(@reflection.through_reflection.klass.name)
]
end
end
"INNER JOIN %s ON %s.%s = %s.%s %s #{@reflection.options[:joins]} #{custom_joins}" % [
@reflection.through_reflection.quoted_table_name,
@reflection.quoted_table_name, reflection_primary_key,
@reflection.through_reflection.quoted_table_name, source_primary_key,
polymorphic_join
]
end
alias_method_chain :construct_joins, :fix
end
end
end
module ActiveRecord
module Reflection
class AssociationReflection
def association_foreign_key_with_fix
@association_foreign_key ||= @options[:primary_key] || class_name.foreign_key
end
alias_method_chain :association_foreign_key, :fix
end
end
end
### EFFECTIVE DIFFERENCE ###
# --- /usr/lib/ruby/1.8/active_record/associations.rb.original 2012-02-25 10:58:08.000000000 -0800
# +++ /usr/lib/ruby/1.8/active_record/associations.rb 2012-02-25 12:19:49.000000000 -0800
# @@ -2096,8 +2096,8 @@
# klass.quote_value(source_reflection.active_record.base_class.name)
# ]
# else
# - first_key = through_reflection.klass.base_class.to_s.foreign_key
# - second_key = options[:foreign_key] || primary_key
# + first_key = source_reflection.association_foreign_key # through_reflection.klass.base_class.to_s.foreign_key
# + second_key = source_reflection.primary_key_name # options[:foreign_key] || primary_key
# end
#
# unless through_reflection.klass.descends_from_active_record?
# @@ -2122,9 +2122,9 @@
#
# " #{join_type} %s ON (%s.%s = %s.%s%s%s%s) " % [
# table_alias_for(through_reflection.klass.table_name, aliased_join_table_name),
# - connection.quote_table_name(parent.aliased_table_name),
# - connection.quote_column_name(parent.primary_key),
# connection.quote_table_name(aliased_join_table_name),
# + connection.quote_column_name(parent.primary_key),
# + connection.quote_table_name(parent.aliased_table_name),
# connection.quote_column_name(jt_foreign_key),
# jt_as_extra, jt_source_extra, jt_sti_extra
# ] +
# --- /usr/lib/ruby/1.8/active_record/associations/has_many_through_association.rb.original 2012-02-25 10:33:53.000000000 -0800
# +++ /usr/lib/ruby/1.8/active_record/associations/has_many_through_association.rb 2012-02-25 12:19:56.000000000 -0800
# @@ -160,7 +160,7 @@
# end
# else
# reflection_primary_key = @reflection.source_reflection.primary_key_name
# - source_primary_key = @reflection.through_reflection.klass.primary_key
# + source_primary_key = @reflection.source_reflection.association_foreign_key
# if @reflection.source_reflection.options[:as]
# polymorphic_join = "AND %s.%s = %s" % [
# @reflection.quoted_table_name, "#{@reflection.source_reflection.options[:as]}_type",
# --- /usr/lib/ruby/1.8/active_record/reflection.rb.original 2012-02-25 10:33:47.000000000 -0800
# +++ /usr/lib/ruby/1.8/active_record/reflection.rb 2012-02-25 12:20:04.000000000 -0800
# @@ -192,7 +192,7 @@
# end
#
# def association_foreign_key
# - @association_foreign_key ||= @options[:association_foreign_key] || class_name.foreign_key
# + @association_foreign_key ||= @options[:primary_key] || class_name.foreign_key
# end
#
# def counter_cache_column
模块活动记录
模块关联
模块类方法
类JoinDependency
阶级联合会
def关联_加入_与_修复
连接=reflection.active\u record.connection
join=case reflection.macro
什么时候:你和你属于很多人吗
“#{join_type}%s在%s.%s=%s.%s上%s”[
表别名(选项[:联接表]、别名联接表名称),
连接。引用表名(别名为连接表名),
选项[:foreign_key]| | reflection.active_record.to_.foreign_key,
连接.引号\u表\u名称(父.别名\u表\u名称),
反射。活动\u记录。主\u键]+
“#{join_type}%s在%s.%s=%s.%s上%s”[
表名称和别名,
连接。引用表格名称(别名表格名称),
klass.primary_键,
连接。引用表名(别名为连接表名),
选项[:关联|外部|密钥]| klass.to _.foreign |密钥
]
什么时候:有很多,:有一个
案例
当反射时。选项[:到]
通过条件=通过反射。选项[:条件]?“和{interpolate_sql(sanitize_sql(通过_reflection.options[:conditions]))}”:”
jt_外键=jt_as_extra=jt_源代码_extra=jt_sti_extra=零
第一个密钥=第二个密钥=as\u extra=nil
if-through_reflection.options[:as]#针对多态联接有许多:through
jt_foreign_key=通过_反射。选项[:as]。到_s+“\u id”
jt_as_extra=“和%s.%s=%s”%[
连接。引用表名(别名为连接表名),
connection.quote_column_name(通过_reflection.options[:as].to_s+“_type”),
klass.quote\u值(父级.active\u记录.base\u类.name)
]
其他的
jt_foreign_key=通过_反射。主_key_名称
结束
case source_reflection.macro
什么时候:你有很多吗
if source_reflection.options[:as]
第一个_键=“#{source_reflection.options[:as]}\u id”
第二个_键=选项[:外键]| |主键
如_extra=“和%s.%s=%s”%[
连接。引用表格名称(别名表格名称),
connection.quote_column_name(“#{source_reflection.options[:as]}_type”),
klass.quote\u值(source\u reflection.active\u record.base\u class.name)
]
其他的
first_key=source_reflection.association_foreign_key#through_reflection.klass.base_class.to_s.foreign_key
second_key=source_reflection.primary_key_name#选项[:foreign_key]| primary_key
结束
除非通过反射。klass。从活动记录下降?
jt_sti_extra=“和%s.%s=%s”%[
连接。引用表名(别名为连接表名),
connection.quote\u column\u name(通过\u reflection.active\u record.heritation\u column),
through_reflection.klass.quote_value(through_reflection.klass.sti_name)]
结束
何时:属于
第一个\u键=主\u键
if reflection.options[:source\u type]
第二个\u键=源\u反射。关联\u外部\u键
jt_source_extra=“和%s.%s=%s”%[
连接。引用表名(别名为连接表名),
connection.quote_column_name(reflection.source_reflection.options[:foreign_type]),
module ActiveRecord
module Associations
module ClassMethods
class JoinDependency
class JoinAssociation
def association_join_with_fix
...
end
alias_method_chain :association_join, :fix
end
end
end
end
end
module ActiveRecord
module Associations
class HasManyThroughAssociation
def construct_joins_with_fix(custom_joins = nil)
...
end
alias_method_chain :construct_joins, :fix
end
end
end
module ActiveRecord
module Associations
module ClassMethods
class JoinDependency
class JoinAssociation
def association_join_with_fix
connection = reflection.active_record.connection
join = case reflection.macro
when :has_and_belongs_to_many
" #{join_type} %s ON %s.%s = %s.%s " % [
table_alias_for(options[:join_table], aliased_join_table_name),
connection.quote_table_name(aliased_join_table_name),
options[:foreign_key] || reflection.active_record.to_s.foreign_key,
connection.quote_table_name(parent.aliased_table_name),
reflection.active_record.primary_key] +
" #{join_type} %s ON %s.%s = %s.%s " % [
table_name_and_alias,
connection.quote_table_name(aliased_table_name),
klass.primary_key,
connection.quote_table_name(aliased_join_table_name),
options[:association_foreign_key] || klass.to_s.foreign_key
]
when :has_many, :has_one
case
when reflection.options[:through]
through_conditions = through_reflection.options[:conditions] ? "AND #{interpolate_sql(sanitize_sql(through_reflection.options[:conditions]))}" : ''
jt_foreign_key = jt_as_extra = jt_source_extra = jt_sti_extra = nil
first_key = second_key = as_extra = nil
if through_reflection.options[:as] # has_many :through against a polymorphic join
jt_foreign_key = through_reflection.options[:as].to_s + '_id'
jt_as_extra = " AND %s.%s = %s" % [
connection.quote_table_name(aliased_join_table_name),
connection.quote_column_name(through_reflection.options[:as].to_s + '_type'),
klass.quote_value(parent.active_record.base_class.name)
]
else
jt_foreign_key = through_reflection.primary_key_name
end
case source_reflection.macro
when :has_many
if source_reflection.options[:as]
first_key = "#{source_reflection.options[:as]}_id"
second_key = options[:foreign_key] || primary_key
as_extra = " AND %s.%s = %s" % [
connection.quote_table_name(aliased_table_name),
connection.quote_column_name("#{source_reflection.options[:as]}_type"),
klass.quote_value(source_reflection.active_record.base_class.name)
]
else
first_key = source_reflection.association_foreign_key # through_reflection.klass.base_class.to_s.foreign_key
second_key = source_reflection.primary_key_name # options[:foreign_key] || primary_key
end
unless through_reflection.klass.descends_from_active_record?
jt_sti_extra = " AND %s.%s = %s" % [
connection.quote_table_name(aliased_join_table_name),
connection.quote_column_name(through_reflection.active_record.inheritance_column),
through_reflection.klass.quote_value(through_reflection.klass.sti_name)]
end
when :belongs_to
first_key = primary_key
if reflection.options[:source_type]
second_key = source_reflection.association_foreign_key
jt_source_extra = " AND %s.%s = %s" % [
connection.quote_table_name(aliased_join_table_name),
connection.quote_column_name(reflection.source_reflection.options[:foreign_type]),
klass.quote_value(reflection.options[:source_type])
]
else
second_key = source_reflection.primary_key_name
end
end
" #{join_type} %s ON (%s.%s = %s.%s%s%s%s) " % [
table_alias_for(through_reflection.klass.table_name, aliased_join_table_name),
connection.quote_table_name(aliased_join_table_name),
connection.quote_column_name(parent.primary_key),
connection.quote_table_name(parent.aliased_table_name),
connection.quote_column_name(jt_foreign_key),
jt_as_extra, jt_source_extra, jt_sti_extra
] +
" #{join_type} %s ON (%s.%s = %s.%s%s) " % [
table_name_and_alias,
connection.quote_table_name(aliased_table_name),
connection.quote_column_name(first_key),
connection.quote_table_name(aliased_join_table_name),
connection.quote_column_name(second_key),
as_extra
]
when reflection.options[:as] && [:has_many, :has_one].include?(reflection.macro)
" #{join_type} %s ON %s.%s = %s.%s AND %s.%s = %s" % [
table_name_and_alias,
connection.quote_table_name(aliased_table_name),
"#{reflection.options[:as]}_id",
connection.quote_table_name(parent.aliased_table_name),
parent.primary_key,
connection.quote_table_name(aliased_table_name),
"#{reflection.options[:as]}_type",
klass.quote_value(parent.active_record.base_class.name)
]
else
foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
" #{join_type} %s ON %s.%s = %s.%s " % [
table_name_and_alias,
aliased_table_name,
foreign_key,
parent.aliased_table_name,
reflection.options[:primary_key] || parent.primary_key
]
end
when :belongs_to
" #{join_type} %s ON %s.%s = %s.%s " % [
table_name_and_alias,
connection.quote_table_name(aliased_table_name),
reflection.klass.primary_key,
connection.quote_table_name(parent.aliased_table_name),
options[:foreign_key] || reflection.primary_key_name
]
else
""
end || ''
join << %(AND %s) % [
klass.send(:type_condition, aliased_table_name)] unless klass.descends_from_active_record?
[through_reflection, reflection].each do |ref|
join << "AND #{interpolate_sql(sanitize_sql(ref.options[:conditions], aliased_table_name))} " if ref && ref.options[:conditions]
end
join
end
alias_method_chain :association_join, :fix
end
end
end
end
end
module ActiveRecord
module Associations
class HasManyThroughAssociation
def construct_joins_with_fix(custom_joins = nil)
polymorphic_join = nil
if @reflection.source_reflection.macro == :belongs_to
reflection_primary_key = @reflection.klass.primary_key
source_primary_key = @reflection.source_reflection.primary_key_name
if @reflection.options[:source_type]
polymorphic_join = "AND %s.%s = %s" % [
@reflection.through_reflection.quoted_table_name, "#{@reflection.source_reflection.options[:foreign_type]}",
@owner.class.quote_value(@reflection.options[:source_type])
]
end
else
reflection_primary_key = @reflection.source_reflection.primary_key_name
source_primary_key = @reflection.source_reflection.association_foreign_key
if @reflection.source_reflection.options[:as]
polymorphic_join = "AND %s.%s = %s" % [
@reflection.quoted_table_name, "#{@reflection.source_reflection.options[:as]}_type",
@owner.class.quote_value(@reflection.through_reflection.klass.name)
]
end
end
"INNER JOIN %s ON %s.%s = %s.%s %s #{@reflection.options[:joins]} #{custom_joins}" % [
@reflection.through_reflection.quoted_table_name,
@reflection.quoted_table_name, reflection_primary_key,
@reflection.through_reflection.quoted_table_name, source_primary_key,
polymorphic_join
]
end
alias_method_chain :construct_joins, :fix
end
end
end
module ActiveRecord
module Reflection
class AssociationReflection
def association_foreign_key_with_fix
@association_foreign_key ||= @options[:primary_key] || class_name.foreign_key
end
alias_method_chain :association_foreign_key, :fix
end
end
end
### EFFECTIVE DIFFERENCE ###
# --- /usr/lib/ruby/1.8/active_record/associations.rb.original 2012-02-25 10:58:08.000000000 -0800
# +++ /usr/lib/ruby/1.8/active_record/associations.rb 2012-02-25 12:19:49.000000000 -0800
# @@ -2096,8 +2096,8 @@
# klass.quote_value(source_reflection.active_record.base_class.name)
# ]
# else
# - first_key = through_reflection.klass.base_class.to_s.foreign_key
# - second_key = options[:foreign_key] || primary_key
# + first_key = source_reflection.association_foreign_key # through_reflection.klass.base_class.to_s.foreign_key
# + second_key = source_reflection.primary_key_name # options[:foreign_key] || primary_key
# end
#
# unless through_reflection.klass.descends_from_active_record?
# @@ -2122,9 +2122,9 @@
#
# " #{join_type} %s ON (%s.%s = %s.%s%s%s%s) " % [
# table_alias_for(through_reflection.klass.table_name, aliased_join_table_name),
# - connection.quote_table_name(parent.aliased_table_name),
# - connection.quote_column_name(parent.primary_key),
# connection.quote_table_name(aliased_join_table_name),
# + connection.quote_column_name(parent.primary_key),
# + connection.quote_table_name(parent.aliased_table_name),
# connection.quote_column_name(jt_foreign_key),
# jt_as_extra, jt_source_extra, jt_sti_extra
# ] +
# --- /usr/lib/ruby/1.8/active_record/associations/has_many_through_association.rb.original 2012-02-25 10:33:53.000000000 -0800
# +++ /usr/lib/ruby/1.8/active_record/associations/has_many_through_association.rb 2012-02-25 12:19:56.000000000 -0800
# @@ -160,7 +160,7 @@
# end
# else
# reflection_primary_key = @reflection.source_reflection.primary_key_name
# - source_primary_key = @reflection.through_reflection.klass.primary_key
# + source_primary_key = @reflection.source_reflection.association_foreign_key
# if @reflection.source_reflection.options[:as]
# polymorphic_join = "AND %s.%s = %s" % [
# @reflection.quoted_table_name, "#{@reflection.source_reflection.options[:as]}_type",
# --- /usr/lib/ruby/1.8/active_record/reflection.rb.original 2012-02-25 10:33:47.000000000 -0800
# +++ /usr/lib/ruby/1.8/active_record/reflection.rb 2012-02-25 12:20:04.000000000 -0800
# @@ -192,7 +192,7 @@
# end
#
# def association_foreign_key
# - @association_foreign_key ||= @options[:association_foreign_key] || class_name.foreign_key
# + @association_foreign_key ||= @options[:primary_key] || class_name.foreign_key
# end
#
# def counter_cache_column