Can a Symbol in ruby create a Column in db table?

291 Views Asked by At

I have a person table with name and email as the columns,

Will the following line create a column "address" in the table:

person = Person.create(:name => "August", :email => "[email protected]", :address => "Maker Street") 
2

There are 2 best solutions below

4
On

Add a new column address to the Person table:

rails g migration AddAddressToPerson address:string

Migrate the database:

rake db:migrate

Create the record:

person = Person.create(:name => "August", :email => "[email protected]", :address => "Maker Street")

Note: You could also use rails g migration AddAddressToPerson address since string is the default datatype.

8
On

Do following steps -

step 1 : First Create new column Address in Person table, by following command -

rails g migration add_address_to_person address:string

step 2 : Migrate database

rake db:migrate

step 3 : Create Person with Address column -

person = Person.create(:name => "August", :email => "[email protected]", :address => "Maker Street")

Above method will insert values in Person table's Name, Email and Address column.