DEV Community

Cover image for Active Record manipulation (column, table. Belongs)
Ahmed R. J. Alsaedi
Ahmed R. J. Alsaedi

Posted on • Updated on

Active Record manipulation (column, table. Belongs)

This blog will guide you on how to create a table change column name, change the datatype, link tables, and rake utilization. First, make sure the rake gem is linked through the Gemfile, once ready use bundle install to get the library to your code. Gemfile is produced with the terminal command bundle init. In javascript, they packages instead of gems, and instead of bundle, you will use npm.

Image description

Use rake -T this will produce all the tools available thanks to the tool library. Next to each command, there will be a little description of what they preform, below I highlighted the most frequently used,

Image description

From here you can create tables, seed information, and drop tables. Below is an example of how to create a table and name it. If you look closely EVA is named WRONG, the right wat would have been """""create_eva"""""".

Image description

The below example is what occurred if create migration is run without a name. Also when creating a table be sure to make the table plural and the links singler. The user of reference to link the table to eva and keanu table. Below picture show example of WRONG USE, observe the double """s""", be careful it is a common mistake especially with code editor suggestions. the CORRECT WAY TO WRITE IT """ references :class_name""""".

Image description

Here is a list of helpful migration tools

create_table :table creation
rename_column :table, :orginal_name, :new_name
change_column :table_name, :column_name, :new_datatype( string, boolean, number)

Next, the data table needs to be filled with information. When a table is filled with information the linking can be helpful and beneficial. For this, we need the seeds.rb file to seed our data.

Producing it is very simple to use the format below as a framework.

ClassName.create(name_key: desrice_valuie)

If a key is not filled with a value it will have nil with searched in the database. Once the table is populated with data the next step will be to link them if there is the desired association. The adam table has a reference to both Keanu and Eva, so the adam table will be linked to those two through the class association and vice versa.

Image description

Latest comments (0)