Today I Learned

TIL, 2017-02-25

  • Apparently, rails-erd bases its diagram off the models, not what a migration does, or what db/schema.rb looks like.
  • Confusion in AR relationships: If the table you are using HAS the foreign key, use has_one. If the other table has the foreign key, use belong_to.
  • A question belongs_to :correct_answer, class_name: Answer so you can do question.correct_answer (no need to add the has_many for the correct answer since you don’t need it yet).
  • Rethinking RSpec for service classes: You can use described_class.new(params), then wrap it around a let block. Example. (The Gitlab source code is very good for reviewing/”midnight reading”.)
  • AR relationships: Don’t forget the validates_presence_of for belongs_to associations.
  • sed for quotation marks (): find . -type f -exec gsed -i '' -e 's/require '"'"'rails_helper'"'"'/wutdufuk/g' {} +
  • I still sort of suck in the shell. I need to be better at shell commands.
  • I really want to build a Rails generator for service objects, since I’m doing it consistently anyway.
  • So when you do a validates_uniqueness_of, you need to add an index to prevent the race condition).

This project is maintained by daryllxd