I’m trying to seed a Rails application with some sql statements in the seed.rb file. There are 12 values supplied, however, the table has 15 columns. The extra three columns are the automatically generated id, created_at and updated at columns that Rails includes by default. If I run a custom sql statement in the seed.rb file in the following manner….
connection = ActiveRecord::Base.connection()
query = "random sql"
connection.execute(query)
Rails doesn’t create those columns for me in the way it would if I did
Employee.create!(name: "Joe")
Is there anyway to indicate to rails that I need the id and timestamp columns filled with values when I run an sql statement in seed.rb?
No, because Rails has no way of knowing whether your
"random sql"even creates any records for it to fill in ids/timestamps.When you
connection.executeyou are on your own, you have forsaken your ORM and given in to the temptation of SQL.If you can do it using ActiveRecord, then do so! If not, well, that is why Rails lets you drop down to SQL (but think again. Can you really not write it in Ruby?).