Skip to content

Instantly share code, notes, and snippets.

View stevehodges's full-sized avatar

Steve Hodges stevehodges

View GitHub Profile
@stevehodges
stevehodges / faker_uniq_demo.rb
Created May 21, 2019 02:26
Faker::Company.name uniqueness
# Demonstration of why you should use Faker's .unique option when using
# Faker on attributes with uniqueness constraints
def first_collision_lazy
names={}
1_000.times do |a|
name = Faker::Company.name
return names.count if names[name]
names[name]=true
end
@stevehodges
stevehodges / attr_encrypted_with_dates.rb
Last active October 7, 2016 14:46
attr_encrypted with Rails multiparameter attributes
class Widget < ActiveRecord::Base
## add data_class option when calling attr_encrypted
attr_encrypted :written_on, key: MY_SECURE_KEY, marshal: true, data_class: Date
end
## This monkeypatch of ActiveRecord ensures that Rails knows that `written_on` is a date when setting it via
## forms/assign_attributes.
##
## The problem:
## When submitted via form, the `written_on` attribute is submitted in parts, with these params: