Skip to content

Instantly share code, notes, and snippets.

@jameswritescode
Created February 26, 2019 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameswritescode/5c6659e89f35f385e07550d92a180be5 to your computer and use it in GitHub Desktop.
Save jameswritescode/5c6659e89f35f385e07550d92a180be5 to your computer and use it in GitHub Desktop.
class Listing < ApplicationRecord
belongs_to :author, class_name: 'Person', inverse_of: :listings
end
class Person < ApplicationRecord
has_many :listings, foreign_key: 'author_id', inverse_of: :author
end
[4] pry(main)> Person.find('c-m9PWhIar54aabagq7ICb').listings.first.author;
Person Load (0.9ms) SELECT `people`.* FROM `people` WHERE `people`.`id` = '1' LIMIT 1
Listing Load (0.5ms) SELECT `listings`.* FROM `listings` WHERE `listings`.`author_id` = '1' ORDER BY `listings`.`id` ASC LIMIT 1
Person Load (1.1ms) SELECT `people`.* FROM `people` WHERE `people`.`id` = '1' LIMIT 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment