Created
January 17, 2017 18:14
-
-
Save pmacaluso3/b71067be12ac6c1e85056be24d61d5f0 to your computer and use it in GitHub Desktop.
from 9/16 advanced activerecord breakout
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 Purchase < ActiveRecord::Base | |
# assumption 1: there is a class called Shirt & a table called shirts | |
# assumption 2: my table has a FK called shirt_id | |
belongs_to :shirt | |
# assumption 1: there is a class called Purchaser & a table called purchasers | |
# assumption 2: my table has a FK called purchaser_id | |
belongs_to :purchaser, class_name: :User | |
end |
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 Shirt < ActiveRecord::Base | |
# assumption 1: there is a table called designers & a class called Designer | |
# assumption 2: i have a FK called designer_id | |
belongs_to :designer, class_name: :User | |
# assumption 1: there is a class called Sale & a table called sales | |
# assumption 2: that table has a FK called shirt_id | |
has_many :sales, class_name: :Purchase | |
# assumption 1: this model has a method called sales | |
# assumption 2: when I call sales, I'll get an array of objects that all have purchaser(s) | |
has_many :purchasers, through: :sales | |
end |
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 User < ActiveRecord::Base | |
# assumption 1: there is a table&class called DesignedShirt | |
# assumption 2: that table has a foreign key called user_id (because I am on the one side, it would be my own class name + _id) | |
has_many :designed_shirts, class_name: 'Shirt', foreign_key: :designer_id | |
# assumption 1: there is a table&class called Purchases | |
# assumption 2: there is a FK on that table called user_id | |
has_many :purchases, foreign_key: :purchaser_id | |
# assumption 1: there is a method called purchases on the current model | |
# assumption 2: when I call the purchases method on this model, I will get a collection of objects. those objects will have a method called purchased_shirts | |
has_many :purchased_shirts, through: :purchases, source: :shirt | |
# assumption 1: this model has a method called designed_shirts | |
# assumption 2: when i call designed_shirts on this model, I will get a collection of objects. Each of those objects will have a method called sales | |
has_many :sales, through: :designed_shirts | |
# assumption 1: this model has a method called purchased_shirts | |
# assumption 2: when i call purchased_shirts on this model, I will get an array of objects that all ahve the purchased_shirts method | |
has_many :clients, through: :designed_shirts, source: :purchasers | |
# assumption 1: the current model has a purchased_shirts method | |
# assumption 2: when i call purchased_shirts on the current model, I will get a collection of objects that have the supported_designers method | |
has_many :supported_designers, through: :purchased_shirts, source: :designer | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment