You need to fix some data so you’ll just iterate through it all and fix it, right?
User.has_purchased(true).each do |customer|
customer.grant_role(:customer)
end
class CatchJsonParseErrors | |
def initialize app | |
@app = app | |
end | |
def call env | |
begin | |
@app.call(env) | |
rescue ActionDispatch::ParamsParser::ParseError => exception |
guard 'rails', :host => "127.0.0.1", :port => '35728' do | |
watch('Gemfile.lock') | |
# Rails | |
watch(%r{app/controllers/.+\.(rb)$}) | |
watch(%r{app/helpers/.+\.rb}) | |
watch(%r{app/mailers/.+\.(rb)$}) | |
watch(%r{app/models/.+\.(rb)$}) | |
watch(%r{app/serializers/.+\.(rb)$}) | |
watch(%r{app/uploaders/.+\.rb}) | |
watch(%r{app/views/.+\.(erb|haml|slim)$}) |
# examples of rails 4 routes concerns | |
concern :commentable do | |
resources :comments | |
end | |
concern :image_attachable do | |
resources :images, only: :index | |
end | |
resources :messages, concerns: :commentable |
The best practice is to use a database based session, which thankfully is very easy with Rails:
Project::Application.config.session_store :active_record_store
# /config/initializers/devise.rb | |
config.password_length = 8..128 | |
# user model validates | |
validate :password_complexity | |
def password_complexity | |
if password.present? and not password.match(/\A(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+\z/) | |
errors.add :password, "must include at least one lowercase letter, one uppercase letter, and one digit" | |
end | |
end |
reference: ruby-journal.com
Install useragent gem by appending to Gemfile:
gem 'useragent'
# With this declaration, Rails will keep the cache value up to date, and then return that value in response to the size method. | |
# without contuer_cache option | |
# asking for the value of @customer.orders.size requires making a call to the database to perform a COUNT(*) query | |
# To avoid this call, you can add a counter cache to the belonging model: | |
belongs_to :customer, counter_cache: true | |
# throught association | |
class Physician < ActiveRecord::Base | |
has_many :appointments |