Dating App With Rails

Ziv Steeven Zamechek
5 min readDec 12, 2016

--

Dating4cause — Dating app connecting singles through volunteer work on shared causes and passion. I will explain how I built API using Ruby on Rails and the MVC pattern, wrote CRUD logic using Ruby on Rails for joining users and activities, and integrated Facebook signup.

I started of prototyping my idea and diving it into two main components. Users and Events. Firstly I created the user model => rails g model User. Edited the migration of the user:

Then I nested the routes of profile and matches inside the users since the profile and matches belong to the users.

Then I installed the Omniauth gem into the application, gem ‘omniauth-facebook’. I created a new initializer file named - Omniauth - under config/initializers folder and add the rack middleware configuration to the application. This is the file where we define our provider with the respective AppID and AppSecret.

Next, notice I need to define as scope the following fields which will be fetch from the user’s Facebook accounts:

  • user_location
  • user_birthday
  • user_about_me
  • email

For security reasons I created an environment file to place our credentials. For this I added a gem called -> dotenv-rails.

Now I can begin creating login/register system. Generating a controller named “Sessions” (rails g controller sessions). In the sessions controller I created 2 methods: Create method (which will login the user) and a Destroy method (which will logout the user).

OmniAuth will always return a hash of information after authenticating with an external provider in the Rack environment under the key omniauth.auth so the first thing we'll do is assign it to a variable -> auth.

Now, assign the returned omniauth hash to a session variable named “Omniauth” . Next, created a method named “sign_in_from_facebook” on the User model and pass in the auth hash as an argument. If the sign_in_from_facebook(auth) method works fine it will return a user object and, if so, let’s assign that user id to a session variable named “user_id”. Finally, just redirect that user to the home page with a notice of “SIGNED IN”.

Now that I can log in a user, I want the ability to log the user out. For that I only have to create a very simple destroy method which basically will set the sessions variables to “nil”. Finally redirect the users back to the home page with a notice of “SIGNED OUT”.

Now we need to find and/or create users accordingly and add the necessary routing for the sessions controller actions. I called method “sign_in_from_omniauth(auth)” directly on the User class, so we must create a class method which begins with “self”. Basically this method tries to find a user through the provider and uid fields, if it find one it return it, if not, call a new method named “create_user_from_omniauth(auth)” and again, pass the auth hash as an argument.

Again making a class method which basically creates a new user with the fields provider, uid, name, gender, date_of_birth, location, bio passed in by the authhash:

Finally, adding routing for the controller. For the session create action we’ll add a facebook callback route. For the session destroy action we’ll add a regular delete request to the sessions destroy action.

Created a private method called “current_user” in the application controller so we can access it in any controller, in order to check if a user is logged in. The method will basically create an instance variable that, if a session variable named “user_id” exists, will return the correspondent user and assign it to the that variable. We then set this method has an helper_method so that we can use it in our views as well.

The second part of my project I had to make friendship association. Generated Friendship Model with the fields:

  • user_id -> integer.
  • friend_id -> integer.
  • state -> string.
  • friended_at -> datetime.

Before running the migration I added a default value to the state field of “pending” because until the users not sent friend request it will be pending.

Now I added the association between users and friendship. So, a friendship belongs_to a user and belongs to a friend. however a friend it's also a user, has we can't have two belongs_to a user relationship on the same model we must say that it belongs to a friend which corresponds to the class User. On the other hand we need to go to our User model we must define that a user has many friendships but also as explained before, we need to be able to check the friendships that you have been requested upon so we’ll need another has_many relationship. We’ll use the same method we did with the Friendship model, by adding an has_many inverse_friendships which corresponds to the class Friendship. In order to return the friends we must define a Foreign Key of friend_id. Also, remember to add the dependent/destroy option so that if a user is deleted all the friendships associated be destroyed as well.

The Event process I created a model with association to the user model as followed:

The event has association with many users and many friendships through users. Then I created the events migration table that included name, location, and price attributes:

In the events controller I included all the CRUD actions:

--

--