How can activerecord objects be edited externally with scripts? I'm currently using the activerecord-import gem to insert objects into the database. Is it possible to edit existing records using this gem or using another tool or gem?
1 Answer
That's a bit unspecific. There are quite a few methods. Here's the best:
You can run rails on the console. Try irb in your project's directory, and you're on a REPL that allows you to run code live. Just try something like:
that_guy = User.find(4)
that_guy.name = "John"
that_guy.save!
(Replace User, 4 and name with a model you have, a record's id and some string attribute).
And of course you can also just run a file of ruby against your project: rails runner your_script.rb
And, if I misunderstood you: you can of course access the database through any other means/languages/libraries.
1 Comment
tadman
You have all the ActiveRecord finder methods at your disposal so
find_by(name: 'Bob') also works. rails runner or a rake task is a good plan for these utility scripts.