Code, Strings, and Keys

    • About Thomas Powell
    • Account
    • It’s been [0] days since [program] last updated
    • Login
    • Password Reset
    • Pianist / Accompanist / Music Director Resume
    • Reading List 2025
    • Register
    • Running Gear – Shoes, Treadmill, and Bandages
    • Site Map
Illustration of a bird flying.
  • Principle of Least Surprise (Astonishment), foreign keys, and Rails

    In yesterday’s post, I sorted through the foreigner gem to figure out how to change the reference column (primary key) that a foreign_key maps to. The problem here is that, unless your Rails project(s) has grown up referencing “natural” primary keys instead of the autoincremented id implicit in an ActiveRecord::Migration, avoiding creating surprise by not…

    April 29, 2014
  • foreigner foreign_key on a different column in source and target table.

    In the foreigner gem, you can specify the foreign_key column on the source table by using the column option. class Cats < ActiveRecord::Migration def change create_table :cats do |t| t.integer :color_id t.integer :second_color_id t.timestamps end add_foreign_key :cats, :colors, column: :second_color_id end end But what about the target table? This can be accomplished using the primary_key…

    April 28, 2014
  • includes and has_many :through associations

    I’ve been trying to mentally absorb how includes and associations work when trying to query a has_many :through association. My understanding at this point: The symbol in the includes() is the same as the association name on whatever model you have. For example: class Account < ActiveRecord::Base has_many :account_destinations has_many :destinations, through: :account_destinations end class…

    April 25, 2014
←Previous Page
1 … 75 76 77 78 79 … 312
Next Page→

Code, Strings, and Keys

Proudly powered by WordPress