Monday, March 24, 2014

Generating a new scaffold in Rails

Recently, Natalie and I had other programming session in which we decided to create a new structure in order to get an easier way to deal with the check-ins interval. This had to be done by splitting the challenge's duration in weeks (instead of considering the whole duration) to keep track of the check-ins made by the user.

We essentially required to create a new Rails model that actually could be directly generated through the Rails console. But instead of using that method, we decided to setup up a scaffold, a tool provided by Rails which helps programmers create structures such as views, models and controllers for a new resource, but only in a single operation. In this case, we had to create a new model, so thanks to the scaffolding method, we were able to do it easily by using the following pattern :

rails g model <model_name> <field_1:type> <field_2:type> … <field_n:type>

The previous command generates a new table in the database with the specified fields and data types. In our case, the model was called "week", and it contained three fields:

rails g model week start_week:datetime end_week:datetime checkins_completed:boolean

Rails console creates the migration file in /db/migrate/ folder, by labeling it with the common patter: <current_date>_create_<model_name>.rb. Rails also executes this migration in our repository, thus, Rails creates the model itself (weeks.rb) in the ./project_name/app/models/ folder, as well as the rspec file (spec/models/week_spec.rb) and the factory file (spec/factories/weeks.rb)

Once we had the new table, we added the current data from other tables (relations) into the new table by following this convention (creating a new migration file):

rails g migration Add<source_field_name1>And<source_field_name2>And<source_field_name_n>To<destination_table> <destination_field_name1>:<type> <destination_field_name2>:<type> ...<destination_field_name_n>:<type>

Note that you need to apply the previous code separately for each one of the tables you want to get any field.

For example, we created two migration files, one per each table of the database we dealt with:

rails g migration AddUserIdAndUserChallengeIdAndCheckinsToWeek user_id:integer user_challenge_id:integer checkins:integer

rails g migration AddWeekIdToHistory week_id:integer

We edited the generated migration files adding the proper information to the new table. For example, this was the code for the Week table:

create_table "weeks", force: true do |t|
t.datetime "start_week"
t.datetime "end_week"
t.boolean "checkins_completed"
t.datetime "created_at"
t.datetime "updated_at"
t.string "user_id"
t.integer "user_challenge_id"
end

Finally, Natalie and I added the active record associations (in our particular case, "belongs_to" and "has_many" associations) into the model files (history.rb and week.rb), and we executed a rake db:migrate command in order to apply all the changes in our local repositories.

No comments:

Post a Comment