Method: Feet::Model::SQLiteModel.create

Defined in:
lib/feet/sqlite_model.rb

.create(initial_hash) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/feet/sqlite_model.rb', line 41

def self.create(initial_hash)
  # Get initial_hash and schema keys without ids and map initial_hash to schema keys
  initial_hash.delete 'id'
  keys = schema.keys - ['id']
  sql_values = keys.map do |key|
    initial_hash[key] ? to_sql(initial_hash[key]) : 'null'
  end

  # Insert values into table
  DB.execute "    INSERT INTO \#{table} (\#{keys.join ','}) VALUES (\#{sql_values.join ','});\n  SQL\n\n  # Build and return the new table entry\n  raw_values = keys.map { |k| initial_hash[k] }\n  data = Hash[keys.zip raw_values]\n\n  # Get the latest id\n  sql = 'SELECT last_insert_rowid();'\n  data['id'] = DB.execute(sql)[0][0]\n\n  self.new data\nend\n"