loading...

Discuss Party

aurangzaibdanial profile image Aurangzaib Danial Liaqat Khan ・6 min read

Ruby makes me happy but Rails takes my happiness to the next level. - Aurangzaib

Today, I have finally deployed discuss party. It took me quite some time on this project but it was all worth it at the end.

After completing my Flatiron school's Rails module, I wanted to try out my newly gained skills in a full project. My initial plan was to make a marketplace and this project was just a sandbox for trying new ideas. But eventually, I realized that as I love writing why not evolve this sandbox into a real project.

What is discuss party?

Discuss party is a reddit + dev.to style app. It allows people to have a discussion on topics they love. But if someone just loves writing, they can mark their topics private and share them with whoever they like.

You must have thought that this blog is going to be about this app but today I really want to share my last six months' experience with Rails.

I completed my Sinatra project in December 2019 and started learning Ruby on Rails in January.
When I started learning Rails, I was just amazed, how friendly and awesome it is. This is something every beginner feels but for me its something more than that.

Programming with Rails, I realized, I want Rails to be my mentor and best friend, not just a stepping stone for quickly moving to Javascript and all of its crazy libraries. So I decided to take it slow and try to drill Rails into myself. I got obsessed with Rails, going deep into every part of it.

I was in intimate relation with Rails. I went through the ActionPack, one by one. Most of my time was spent in ActiveRecord where I really tried to go deep. I even tried to mimic ActiveRecord, check it out. In the process, I learned crazy things like AREL through which I was able to write SQL queries that can't be written using a regular ActiveRecord query interface. Consider the following code

class User < ActiveRecord
  def age_is_greater_than_or_equal_to_18
    arel[:table][:age].gteq
  end
end

User.where(age_is_greater_than_or_equal_to_18)
#normally
User.where('age >= 18') # ughhh

Look at Arel's implementation, it is so colloquial to read and work with. Also, it has the added benefit of using the right table name if we are using aliasing.

When I'm programming with Ruby and Rails, it will feel like, I'm writing a book or telling a story. It feels like I'm a software writer instead of a software developer, just like how DHH puts it -Aurangzaib

Moving on, there is just so much good offered by ActiveRecord. If you can really get comfortable with it, you can achieve so much that others achieve after hours of hard work. Considering the problem of the N+1 query, ActiveRecord equips you with everything that you need to solve.

Below is the snippet of code that I use to display a feed of topics for users on the home page.

def for_list_view(order_type:, current_user:, visibility:, page_number:)
  topics = includes_vote_count.
  where(visibility: visibility).
  page(page_number).
  add_order(order_type).
  includes(:tags, creator: {display_picture_attachment: :blob})

  topics = filter_reported(topics, current_user) if current_user

  find_votes_for(topics, current_user)
end

Look at that, do you see those includes(:tags, ...) I can't imagine how much time do these methods save me.

Let's talk about migrations for a second, one day I was working on my todo-mvc app and I had a relation of Users having many Lists. As I was in the middle of the app, I realized I wanted to alias User as a Creator for Lists.

class List
  belongs_to :creator, class_name: 'User'
end

I had to rename user_id column in my lists table to creator_id but I also had a DB index on user_id, so I was like feeling really lazy that I had to rename a column and an index and I really did not know to how to rename an index and how to make creator_id column reference id in users table. So I just created a migration for renaming the column to creator_id and just hoped it would even rename the index. After the migration ran, I was blown away because it even renamed the index. Man, at that point I was filled with so much love, awe, and respect for Rails. I just felt like reaching out to everyone who contributes to Rails and give them a hug because they really put sincere effort into whatever they build.

That is a lot about ActiveRecord but that's not just it. Filter pattern in controllers, file management, there is just so much awesomeness.

What I think about Rails is, apart from helping you deploy apps quickly, if you really focus on its API and how it works then you can really become a better developer. For example, the way there is a structure to everything in Rails, everything is organized from A-Z. If you can really drill this concept of organization then you can use these techniques in any language or framework.

I really want to share how awesome ActionView is. The helpers are incredible for managing view logic. Layouts, partials really make your views organized to the core. I do not like long files, I like to keep them to a bare minimum so I do not have to scroll a lot. Consider the following code

<%= title 'Discuss Party' %>

<%= render(@topics) %>

Look at that, if you look at the home page, it has so many stuff on it but the 'erb' file is a treat to look at and it makes you feel not to be overwhelmed when making an edit.

Let's talk about controllers, looking at a skinny Rails controller is a treat. Because the flow of execution is clear and you can easily find out what's going to happen in a request. Also, reading filters or hooks underneath class definition feels like reading a story.

class TopicsController < ApplicationController
  before_action :authenticate_user!, except: :show
  before_action :set_topic, except: %i[new create]
  before_action :reported_by_current_user?, only: %i[show vote]
  before_action :topic_is_private?, only: :sharing
  before_action :authorize_action, except: %i[new create]

# continue 

There is just so so much more and I really want to talk about everything but that is going to make this blog post super long.

Finally, the last thing that I achieved through this project is the gift of test-driven development. I never would have been able to complete this project without the help of TDD. For example, the voting feature of topics has so many edge cases that your mind starts to blow off. You can't really make a confident change if you haven't written tests. Initially, I was worried about testing but then I just did it and I did not care if I did it the right way or not. I still do not know anything about stubs, mocks, etc. But I just do not care, as long as I can test something or anything, I think I will be able to learn these pretty soon.

RSpec is the friendliest framework for testing and it really allows us to write down your requirements like a list of groceries -Aurangzaib

Also, when I started testing for this project, I was trying to test every aspect of it that made my progress slow. But I think the idea is to test those components that you think are going to be a drag while manually testing.

In conclusion, I just want to say that it has been a blast with Rails. I'm really really thankful to God that I came across something that just really fits my head and personality. I'm going to dive into JavaScript now and I'm confident that I will be able to go through it like a breeze because Rails has really made me comfortable in terms of thinking for the solutions of problems.

I'm planning to take my Rails skills to the next level by learning advanced courses offered at upcase by thoughbot for FREE!

Posted on by:

aurangzaibdanial profile

Aurangzaib Danial Liaqat Khan

@aurangzaibdanial

Hi, my nickname is Sunny and sometimes Dani. Ruby on Rails enthusiast and my favorite programmer is Avi Flombaum!

Discussion

markdown guide