Category: daily learning

  • rspec before(:all) is tempting but stubs don’t play nicely with it.

    I was looking at an rspec test with multiple expectations in the same example. before(:all) do SpecialClient.any_instance.stub(:login).and_return { true } end # # MUCH SPACE # # WOW # it “does stuff successfully” do # preceding code, including controller action expect(result.valid?).to be_true expect(response.body).to match /All is happy/ expect(response.code).to eq “200” end I wanted to add…

  • Conditions on a has_many :through and ActiveRecord 4.1 enum

    Say you have a set of models (model names sanitized for generic posting) where the join table in the middle of a has_many :through has an enum that you want to add a filtered association for. class Student < ActiveRecord::Base has_many :student_grades has_many :grades, through: :student_grades end class StudentGrade < ActiveRecord::Base enum approval_status: [:not_reviewed, :rejected,…

  • has_many :through, self referential models

    Hat tip to [has_many :through self-referential example] that sent me down the correct path on this. The challenge I had went a little deeper than the aforementioned post, however. I’m going to reuse the friend example, but with a twist. class Person < ActiveRecord::Base # id :integer end class Friendship < ActiveRecord::Base # source_friend_id :integer…