TIL, 2017-04-07, Where Um
- Multiple email addresses for the same account–um why?
validate_false:
Allows you to do acreate(:user, :validate_false)
(create a user but skip validations) Reference
FactoryGirl.define do
factory :user do
email { Faker::Internet.email }
trait :validate_false do
to_create { |instance| instance.save(validate: false) }
end
end
end
The Elements of Style in Ruby #13: Length vs Size vs Count
- Good idea:
size
for collections andlength
for strings, since hashes and stacks don’t have a length but a size. count
can be invoked with a block or argument.
arr = [1, 1, 2, 3, 5, 6, 8]
arr.count(&:even?) # => 3
arr.count(1) # => 2
- AR::Relation
count
will perform a count query,length
will load up the records, andsize
will do the smart thing based on whether it is loaded. - Set or Hash are faster with
length
andsize
vs.count
.