Skip to content

Instantly share code, notes, and snippets.

@thorvn
Last active July 20, 2018 06:39
Show Gist options
  • Save thorvn/b36eb20c0a784be62e6ef398776f34eb to your computer and use it in GitHub Desktop.
Save thorvn/b36eb20c0a784be62e6ef398776f34eb to your computer and use it in GitHub Desktop.
Conding convention

1. Hạn chế việc sử dụng if else lồng nhau. Ở đầu function nên return các case sai sau đó mới viết logic cho function

2. Khi viết SQL không được nối chuỗi mà phải sử dụng param truyền vào câu truy vấn.

3. Sử dụng Active record hoặc Arel over raw queries

Example:

    # unless ids.blank?
    #   sql = "DELETE FROM links WHERE links.user_id = #{user_id.to_s} AND links.id IN (#{ids.to_s})"
    #   connection.execute(sql)
    # end
    return if ids.blank?
    where('user_id = ? AND id IN (?)', user_id, ids).delete_all

4. Với các truy vấn SELECT thì sử dụng Scope over class method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment