1

I am trying to insert data into a SQLite database that is used for a Rails application.

I initially thought I could have used SQLite manager and insert into tablename (value) values ('value') however, it did not insert anything into the table and did not produce an error.

Am I doing anything wrong? Is there a way to do this directly with Rails?

I thought I could use some sort of activerecord migration and just rake db:migrate, however I have been unable to find the appropriate commands.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
jshbrmn
  • 1,737
  • 3
  • 22
  • 52
  • 1
    Did you flush/commit the SQLLite command? In any case, you can also use the Rails console using normal ActiveRecord model calls, or via migration--without knowing what you did, it's impossible to know why it didn't work. – Dave Newton Jan 09 '13 at 19:50
  • `values('value)` -- is it a typo in question, or really missing apostrophe in the query? (should be values('value')) – Anton Kovalenko Jan 09 '13 at 19:51
  • 1
    You could try to discover `rake db:seed` if you are looking for direction. – Yevgeniy Anfilofyev Jan 09 '13 at 19:54
  • Anton, that was merely a typo :p – jshbrmn Jan 09 '13 at 20:24
  • Based on the information provided by Yevgeniy Anfilofyev, I went with `rake db:seed` as my approach to solving this. It was easy and provides a way of staying within the RoR environment and still manage and edit SQlite databases. – jshbrmn Jan 10 '13 at 14:14

1 Answers1

1

rails runner is a nice way to leverage the Rails runtime, without needing to load the entire Rails stack for your application. It'll give you the full ActiveRecord resources for your underlying DB, making it easy to do database operations.

From the runner built-in help:

rails runner
Usage: runner [options] ('Some.ruby(code)' or a filename)

    -e, --environment=name           Specifies the environment for the runner to operate under (test/development/production).
                                     Default: development

    -h, --help                       Show this help message.

You can also use runner as a shebang line for your scripts like this:
-------------------------------------------------------------
#!/usr/bin/env /Users/greg/junk/foo/script/rails runner

Product.all.each { |p| p.price *= 2 ; p.save! }

I've used this several times for jobs that loaded data underneath Rails into the database. It'd be a great solution for what you need to do.

"Rails task: script/runner or rake?" is worth reading too for more information.

Community
  • 1
  • 1
the Tin Man
  • 158,662
  • 42
  • 215
  • 303