nulltek
nulltek

Reputation: 3337

Mass updating of database fields from Rails console

I have a Rails 3.2.19 app where I have a call model and a region model.

Call belongs_to :region and has region_id in the table Region has_many :calls and the field I'm interested in is in the Region model called area which is a string type that contains a City (Houston, Dallas).

My question is. I have nearly 18K call records where I need to update the region_id to the region's area. i.e "1" or "2"

What's the best way to update this region_id field via the console or even better yet via PSQL which we use as a database?

I know this is probably a simple thing but I want to make sure I don't cause problems in production.

Upvotes: 0

Views: 848

Answers (1)

sufleR
sufleR

Reputation: 2963

Use update_all

Call.where(area: 'Houston').update_all(region_id: 1)

To update calls which doesn't have region_id

Call.where("region_id IS NULL").update_all(region_id: 1)

Upvotes: 3

Related Questions