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.

5
  • 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. Commented Jan 9, 2013 at 19:50
  • values('value) -- is it a typo in question, or really missing apostrophe in the query? (should be values('value')) Commented Jan 9, 2013 at 19:51
  • 1
    You could try to discover rake db:seed if you are looking for direction. Commented Jan 9, 2013 at 19:54
  • Anton, that was merely a typo :p Commented Jan 9, 2013 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. Commented Jan 10, 2013 at 14:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the information. I ended up using rake db:seed as my approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.