Tag: rails

  • Nesting resource routes with alternate scopes and keys + strong parameters.

    DISCLAIMER: This is a contrived example translated from a case where it actually made way more sense to break the familiar nested resource pattern. Also, I haven’t double-checked how Rails’ pluralization rules would affect the naming of any given Species. Say you have a pair of models: class Species # id # latin_name_with_underscores end class…

  • 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…