Last modified: Thu Aug 16 2018 22:49:02 GMT+0800 (Malay Peninsula Standard Time)
In the previous chapter, we created a simple car reservation system. However, the bookings are useless if they are not associated with any user. In order make sure the booking is assigned to the logged in user, we have to modify the model and controller. You may use the similar concept to apply it to any model and controller.
To begin, let’s add a user_id column in our Booking model. Figure 5.1.1 shows the code in Table 5.1.1 is added to Line 8.
Table 5.1.1: Code to add to DB
#MyApp/app/db/migrate/YYYYMMDDHHMMSS_create_bookings.rb t.belongs_to :user, foreign_key: true
The code in Table 5.1.1 is saying that we are adding a column that have a foreign key that belongs to my
User model. If you name your
User model differently, you have to rename
:user to your respective model.
YYYYMMDDHHMMSS is the date and time of the file is created.
Figure 5.1.1: Code to add to Booking Table, Line 8
Add the following code in Table 5.1.2 to file respective file. Figure 5.1.2 shows the result of the operation shown in Table 5.1.2. operation is shown below.
Table 5.1.2: Code to add to
#MyApp/app/controllers/bookings_controller.rb #add after permit .merge(user_id: current_user.id)
Figure 5.1.2: Code to add to
Controller, Line 16
You may not have
current_user.id defined if you are not using
Devise Gem. Please follow the next section, Chapter 5.2, or skip the section if you have it defined. If you are getting an error saying that
current_user is not defined, you have to add to follow the instruction in Chapter 5.2.
development.sqlite3 and migrate your database once you are done modifying the file. You may have to turn off your rails server and SQLiteStudio if you have it opened before deleting
You may have seen a lot of tutorials online and wonders why people are using
current_user and where does it actually come from. If you are using
devise Gem to model your
current_user is already built in in the Gem. What if you are not using the devise Gem and still want to have the ability to use the method? In this case, we have to add the method manually. Follow the instruction in Table 5.2.1.
Table 5.2.1: Code to add to Controller
#MyApp/app/controllers/application_controller.rb class ApplicationController < ActionController::Base helper_method :current_user def current_user return unless session[:user_id] @current_user ||= User.find(session[:user_id]) end end
I have not tested the code In Table 5.2.1 in my Application since I have my
User model generated by
Devise gem. However, I have used this code earlier. In order for the code to function properly, you have to have a
user logged it when
Since we added a new column to the
User model, we have to delete the development.sqlite3 so that we can migrate the database and rebuild the table. In order to delete the file, you may have to close your SQLiteStudio and your Rails server. Now, start your server and create your booking with a logged in user.
Figure 5.3.1 shows the newly rebuild database with
user_id field and the
user_id is recorded and stored in the database when a booking is created by the user.
user_id filled will be filled in automatically as we have a
current_user declared in Application Controller (more like a global method) and can be used in any controller. The method,
current_user.id, will return the current logged in user ID.
Figure 5.3.1: Booking is now associate to currently logged in user