0

How to add new column and update its value from another table's column with conditions ?

I'm having two tables named user

id: integer
name: string
preferred_communication_channel_id: integer  // stores the id of communication_channel

and table communication_channel stores different communication channels like Email,Telephone,etc.. of user

id: integer
user_id:  integer 
type: string

Now I need to add new column communication_mode in the user table

And update the column communication_mode value from communication_channel.type with some constraints

Here goes the mysql version of the update

update user as p
join communication_channel as cc 
on p.preferred_communication_channel_id = cc.id and p.id=cc.user_id and cc.type="Telephone" 
set p.communication_mode=3

I've created a migration to add new column but how to implement this query in rails migration ?

Please correct me if I missed anything

Siva
  • 7,780
  • 6
  • 47
  • 54

1 Answers1

0

In migrations you can just write execute "SELECT * FROM bananas" and the SQL will be executed!

RobHeaton
  • 1,390
  • 1
  • 9
  • 13