instance method
insert_fixture
Ruby on Rails 5.2.8.1
Since v2.2.3Signature
insert_fixture(fixture, table_name)
Inserts the given fixture into the table. Overridden in adapters that require something beyond a simple insert (eg. Oracle). Most of adapters should implement insert_fixtures that leverages bulk SQL insert. We keep this method to provide fallback for databases like sqlite that do not support bulk inserts.
Parameters
-
fixturereq -
table_namereq
Source
# File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 343
def insert_fixture(fixture, table_name)
fixture = fixture.stringify_keys
columns = schema_cache.columns_hash(table_name)
binds = fixture.map do |name, value|
if column = columns[name]
type = lookup_cast_type_from_column(column)
Relation::QueryAttribute.new(name, value, type)
else
raise Fixture::FixtureError, %(table "#{table_name}" has no column named #{name.inspect}.)
end
end
table = Arel::Table.new(table_name)
values = binds.map do |bind|
value = with_yaml_fallback(bind.value_for_database)
[table[bind.name], value]
end
manager = Arel::InsertManager.new
manager.into(table)
manager.insert(values)
execute manager.to_sql, "Fixture Insert"
end
Defined in activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb line 343
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::ConnectionAdapters::DatabaseStatements