Mysql: increase the size of innodb_buffer_pool_size

By putting this on your mysql config file: [mysqld] innodb_buffer_pool_size = 2G In dedicated db machines, you can set this to close to 80% of the available memory. This will avoid disk writes and speed up things in general. The mysql configuration file is on : /etc/my.cnf (usually) Restaring mysql, and barabum barabim The typical […]

Ruby On Rails: cookies

cookies[:remember_token] = { :value => user.id, :expires => 20.years.from_now.utc } A “remember token” cookie is created, you can also do the 20 years part as: cookies.permanent[:remember_token] = user.remember_token cookies.permanent.signed[:remember_token] = [user.id, user.salt] Obscures the real user.id with the salt, for security purposes Retrieving stuff from cookies: User.find_by_remember_token(cookies[:remember_token]) // This will query for a user with remember_token

Ruby On Rails: forms

In the example below we are passing a @user variable, but you can also pass something like this: <%= form_for User.new, :url => { :controller => “users”, :action => “create” }, :html => {:class => “whatever_class_needed”} do |f| %> And now, the example: <%= form_for(@user) do |f| %><%= f.label :name %> <%= f.text_field :name %><%= f.label :email %> <%= f.text_field :email %><%= f.label :password %> <%= f.password_field :password %><%= f.label :password_confirmation, “Confirmation” %> <%= f.password_field :password_confirmation %> <%= f.submit “Create my account”, class: “btn btn-large btn-primary” %> <% end %> If […]

Ruby On Rails: manually generating a migration. Enforce uniqueness of a database column

$ rails generate migration add_email_uniqueness_index It will create the following file: db/migrate/_add_email_uniqueness_index.rb In this case, we are adding an index to the email address column, to guarantee its uniqueness, so the file will look like this: class AddEmailUniquenessIndex < ActiveRecord::Migration def self.up add_index :users, :email, :unique => true end def self.down remove_index :users, :email end […]

Ruby On Rails: utilities and console commands

run rails as in prod mode (in your dev env) $ bundle exec rake assets:precompile $ bundle exec rake db:migrate RAILS_ENV=production change this line inside: config/environments/production.rb   config.serve_static_files = true run your server as: SECRET_KEY_BASE=`rake secret` rails s -e production -b 0.0.0.0 the -b part is only if you are running inside vagrant and port forwarding. console […]

Ruby On Rails: models

$ rails generate model User name:string email:string invoke active_record create db/migrate/_create_users.rb create app/models/user.rb invoke rspec create spec/models/user_spec.rb Note: Make sure you assign what attributes of your model will be accessible for edit, otherwise Ruby assumes all of them are, and this can be a security hole: class User < ActiveRecord::Base attr_accessible :name, :email Note: on […]