Skip to content

Instantly share code, notes, and snippets.

@Bahanix
Bahanix / n_plus_one.rb
Last active March 22, 2017 12:06
N+1 queries example
class User < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :user
end
# It will hit 11 times your database, that's bad!
User.limit(10).each do |user|
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :set_locale
private
def set_locale
if session[:locale]
I18n.locale = session[:locale]
@Bahanix
Bahanix / index.html.erb
Last active June 2, 2016 09:21
Ruby on Rails authorization exemple
# app/views/users/index.html.erb
<h1>Liste des utilisateurs</h1>
<ul>
<% @users.each do |user| %>
<li><%= user.id %></li>
<% end %>
</ul>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
= simple_form_for(@account) do |f|
= f.error_notification
= f.input :type, collection:['iban', 'rib'], include_blank: false
#iban
.inputs
= f.input :iban, :include_blank => false
= f.input :bic, :include_blank => false
@Bahanix
Bahanix / git.sh
Last active August 29, 2015 14:20
Commandes de base pour git
### Configurer git et vim
git config --global core.editor "vim"
vim ~/.vimrc
syn on
set tabstop=2
set shiftwidth=2
set expandtab
match Todo /\s\+$/