views:

155

answers:

3

I'm using Netbeans to develop my RoR project so it is managing the SQL database. How can I make quick changes (i.e. edit row-by-row) to my DB, preferably in command line?

i'm thinking - changing temporary passwords and users for testing purposes. Thanks for your input!

A: 

You could just use the direct mysql interface, but I'd use script/console to go through your model classes unless you really need direct DB access.

Chuck
+3  A: 

Two ways:

  • run script/console and manipulate your Rails' model objects directly from the command line

  • run script/dbconsole which will drop you into the command line for your RDBMS (assuming that your database.yml file is configured to access your database correctly). Then use SQL to do what you need to do

(Railscast on Console Tricks)

John Topley
i've used script/console to query the DB in the past, but have have have encountered "undefined method" exception when attempting to save objects. Is this probably a syntactical error or something more fundamental? This is what I have been trying:prompt> Object.save("name" => "john", "password" => "foo"... ETC)
happythenewsad
You want Object.create("name" => "john", "password" => "foo")
Topher Fangio
You should be doing o = Object.create(:name => "john", :password => "foo"), followed by o.save
John Topley
The save method optionally accepts only one parameter, which is a boolean that indicates whether validation should be performed or not.
John Topley
@John Topley - Object.create automatically calls Object.save, Object.new doesn't ;-)
Topher Fangio
@Topher - good call, I always forget about that!
John Topley
+2  A: 

Try using ruby script/console in your rails application directory. From there, you can do things like:

u = User.find(:first)
u.password = 'something_else'
u.save

or

users = User.find(:all)
users.each { |u| u.password = 'something'; u.save }

which will update all users' passwords.

Topher Fangio
You can also create new objects a la User.create(:name => "Foo", :active => true).
runako