Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Large Hardon Collider
Nov 28, 2005


PARADOL EX FAN CLUB
I'm brand new to rails. What's the "right way" to change just one field in a database table? I want to do

UPDATE ratings SET text="blahblah" WHERE text="balhblah"

I thought that the right way would be through generating a migration, but it looks like that's just for updating the structure of the database, rather than the contents (correct me if I'm wrong). How do I change this one field, besides doing it manually through dbconsole (which would screw up the versioning)?

Adbot
ADBOT LOVES YOU

Large Hardon Collider
Nov 28, 2005


PARADOL EX FAN CLUB

prom candy posted:

Checkout ActiveRecord's update_all method. You're trying to update a number of records, correct?
Actually just one. The site lists local businesses, each with a number of aspects you can rate, formulated as questions. The questions themselves are stored in the database, and I want to correct a typo in one of them.

Large Hardon Collider
Nov 28, 2005


PARADOL EX FAN CLUB

prom candy posted:

So you're looking to do the most basic update operation then. Open up your console and do something like.
code:

id = <id of the rating>
rating = Rating.find(id)
rating.field = value
rating.save
Alternately:

code:

Rating.find(<id of the rating>).update_attributes(:field => value)

I don't mean to be a dick, but this is really basic stuff that you should have learned from a beginner's tutorial. It's going to be a long, slow learning process if you don't do some up-front reading on the basics.
No offense taken. I should have mentioned that I'm working with git and separate staging and production servers, which is a first for me. Usually I'd manually edit the database, but I know that I need to do something in the rails app so it will be consistent across all our environments.

Also, the database was migrated from the old php setup, so the values in this table aren't derived from a seed file I can edit.

I thought that the appropriate way to do this was to make a db:migrate operation, but it seems like that's for changing database structures, not contents. Is that assumption wrong? Otherwise, I suppose I will make a custom rake task, but that means that I will have to manually run it in each environment, right?

Thanks for your help, guys. I started learning this stuff just two days ago, and I know I've got a lot to learn.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply