Created
February 26, 2019 23:23
-
-
Save jameswritescode/5c6659e89f35f385e07550d92a180be5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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