未验证 提交 31062c9d 编写于 作者: R Ryuta Kamizono 提交者: GitHub

Merge pull request #39292 from kamipo/type_cast_pluck_without_table_name_qualified

Type cast `pluck` values for table name unqalified column in joins tables
......@@ -131,6 +131,10 @@ def apply_column_aliases(relation)
relation._select!(-> { aliases.columns })
end
def each(&block)
join_root.each(&block)
end
protected
attr_reader :join_root, :join_type
......
......@@ -410,14 +410,34 @@ def type_for(field, &block)
@klass.type_for_attribute(field_name, &block)
end
def build_join_dependencies
join_dependencies = []
join_dependencies.unshift construct_join_dependency(
select_association_list(joins_values + left_outer_joins_values, join_dependencies), nil
)
end
def lookup_cast_type_from_join_dependencies(name, join_dependencies)
join_dependencies.each do |join_dependency|
join_dependency.each do |join|
type = join.base_klass.attribute_types.fetch(name, nil)
return type if type
end
end
nil
end
def type_cast_pluck_values(result, columns)
join_dependencies = nil
cast_types = if result.columns.size != columns.size
klass.attribute_types
else
columns.map.with_index do |column, i|
column.try(:type_caster) ||
klass.attribute_types.fetch(name = result.columns[i]) do
result.column_types.fetch(name, Type.default_value)
join_dependencies ||= build_join_dependencies
lookup_cast_type_from_join_dependencies(name, join_dependencies) ||
result.column_types[name] || Type.default_value
end
end
end
......
......@@ -729,7 +729,9 @@ def test_pluck_type_cast
assert_equal [ topic.approved ], relation.pluck(:approved)
assert_equal [ topic.last_read ], relation.pluck(:last_read)
assert_equal [ topic.written_on ], relation.pluck(:written_on)
end
def test_pluck_type_cast_with_conflict_column_names
expected = [
[Date.new(2004, 4, 15), "unread"],
[Date.new(2004, 4, 15), "reading"],
......@@ -743,6 +745,29 @@ def test_pluck_type_cast
assert_equal expected, actual
end
def test_pluck_type_cast_with_joins_without_table_name_qualified_column
assert_pluck_type_cast_without_table_name_qualified_column(Author.joins(:books))
end
def test_pluck_type_cast_with_left_joins_without_table_name_qualified_column
assert_pluck_type_cast_without_table_name_qualified_column(Author.left_joins(:books))
end
def test_pluck_type_cast_with_eager_load_without_table_name_qualified_column
assert_pluck_type_cast_without_table_name_qualified_column(Author.eager_load(:books))
end
def assert_pluck_type_cast_without_table_name_qualified_column(authors)
expected = [
[nil, "unread"],
["ebook", "reading"],
["paperback", "read"],
]
actual = authors.order(:last_read).where.not("books.last_read": nil).pluck(:format, :last_read)
assert_equal expected, actual
end
private :assert_pluck_type_cast_without_table_name_qualified_column
def test_pluck_with_type_cast_does_not_corrupt_the_query_cache
topic = topics(:first)
relation = Topic.where(id: topic.id)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册