insert_fixture(fixture, table_name)
public
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.
Show source
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