From: Josh Cheek Date: 2010-01-07T05:24:55+09:00 Subject: Re: Script to interact with a database --000e0cd13806422369047c84bfb7 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Jan 6, 2010 at 9:51 AM, Quee WM wrote: > I have an application in rails in which i am adding a new column in one > of the tables. The value for the new column is a bit.ly link and I need > to populate this for each record in the table approx. 250 rows will be > affected. > > Q1: Can this be done outside the application? i.e. write a script that > can interact with the db and then populate the appropriate column? And > is this advisable? > > Q2: if the aforementioned idea is not a good way then what will be the > best way to update the existing records as any code i will write will > only be used to update the table once. > -- > Posted via http://www.ruby-forum.com/. > > I'd make a method in your model that gets the address, then have it get that address upon creating a record. For all the records currently in there, just go to the console, iterate through each, and invoke that method on them. To make the method that updates it, you can use the api, or a gem: http://code.google.com/p/bitly-api/wiki/ApiDocumentation http://gemcutter.org/search?query=bit.ly For a 1 time edit with 250 records, I'd use the console script/console , for recurring edits, I'd create a rake task http://railscasts.com/episodes/66-custom-rake-tasks If you have a large number of records, you might need to do something different, but I'm not sure how to handle that. Sometimes, due to constraints within your app's model, you need to edit the db directly, I use sequel pro http://www.sequelpro.com but this really almost never occurs (or at least shouldn't). --000e0cd13806422369047c84bfb7--