0

I wanted to know what is the right way to update a Model.
for example, suppose I want to change a name of a data member from likes to numOfLikes.

this variable appears in the Model itself, but also in schema.rb and in db\migrate\XXX.rb

I changed manually those files and got this error:

undefined method `numOfLikes' for # Topic:0x3442d88

So, what is the right way doing it? (I am also asking about deleting a data member or adding one)

1

1 Answer 1

2

The best way is to run a migration to rename the column, which will update the schema.rb file.

You shouldn't be editing schema.rb directly, and the migration file only runs commands on the database, not affect the model if changed once run.

Schema.rb represents the state of the database schema, it doesn't control or change it by changing the contents of the file alone.

A new migration that contains:

rename_column :table_name, :likes, :numOfLikes

This will rename the column in the database, and will dump the database schema into schema.rb with the new attribute name.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.