{"id":3931,"date":"2015-05-05T08:27:57","date_gmt":"2015-05-05T12:27:57","guid":{"rendered":"http:\/\/webninjataylor.com\/library\/?p=3931"},"modified":"2015-05-05T11:44:09","modified_gmt":"2015-05-05T15:44:09","slug":"ruby-on-rails-associations","status":"publish","type":"post","link":"https:\/\/webninjataylor.com\/library\/ruby-on-rails-associations\/","title":{"rendered":"Ruby on Rails: Associations"},"content":{"rendered":"<ul>\n<li>Associations between models make common operations simpler and easier\n<pre>class Customer &lt; ActiveRecord::Base\r\n has_many :orders, dependent: :destroy\r\nend\r\n \r\nclass Order &lt; ActiveRecord::Base\r\n belongs_to :customer\r\nend<\/pre>\n<\/li>\n<li>By declaring that one model belongs_to another, you instruct Rails to maintain Primary Key-Foreign Key information between instances of the two models, and you also get a number of utility methods added to your model&#8230;\n<ul>\n<li><span class=\"code\">belongs_to<\/span> &#8230; sets up a one-to-one connection with another model (the belong_to model declared needs to be singular e.g. <span class=\"code\">:customer<\/span>)<\/li>\n<li><span class=\"code\">has_one<\/span> &#8230;\u00a0sets up a one-to-one connection with another model (opposite direction from\u00a0belongs_to)<\/li>\n<li><span class=\"code\">has_many<\/span> &#8230; one-to-many connection with another model, often found on the other side of a belongs to association<\/li>\n<li><span class=\"code\">has_many<\/span> :through &#8230; many-to-many connection indicating that the declaring model can be matched with zero or more instances of another model by proceeding through a third model&#8230;\n<pre>class Physician &lt; ActiveRecord::Base\r\n has_many :appointments\r\n has_many :patients, through: :appointments\r\nend<\/pre>\n<\/li>\n<li><span class=\"code\">has_one<\/span> :through &#8230; one-to-one\u00a0connection indicating that the declaring model can be matched with an\u00a0instance of another model by proceeding through a third model<\/li>\n<li><span class=\"code\">has_and_belongs_to_many<\/span> &#8230; a direct many-to-many connection<\/li>\n<\/ul>\n<\/li>\n<li><span class=\"code\">belongs_to<\/span> vs.\u00a0<span class=\"code\">has_one<\/span>\n<ul>\n<li>The distinction is in where you place the foreign key (it goes on the table for the class declaring the belongs_to association)<\/li>\n<li>Should make common sense via ownership (e.g. a user owns an account, not an account owns\u00a0a user)<\/li>\n<\/ul>\n<\/li>\n<li><span class=\"code\">has_many :through<\/span> vs. <span class=\"code\">has_and_belongs_to_many<\/span>\n<ul>\n<li>The simplest rule of thumb is that you should set up a has_many :through relationship if you need to work with the relationship model as an independent entity<\/li>\n<li>If you don&#8217;t need to do anything with the relationship model, it may be simpler to set up a has_and_belongs_to_many relationship (though you&#8217;ll need to remember to create the joining table in the database)<\/li>\n<li>You should use has_many :through if you need validations, callbacks, or extra attributes on the join model<\/li>\n<\/ul>\n<\/li>\n<li>Use advanced polymorphic associations when a model can belong to more than one other model (different from has_many)<\/li>\n<li>Self joins for a single model with internal relationships (e.g. employees table with managers and subordinates)\n<pre>class Employee &lt; ActiveRecord::Base\r\n has_many :subordinates, class_name: \"Employee\",\r\n   foreign_key: \"manager_id\"\r\n belongs_to :manager, class_name: \"Employee\"\r\nend<\/pre>\n<\/li>\n<li>If you create an association some time after you build the underlying model, you need to remember to create an add_column migration to provide the necessary foreign key<\/li>\n<\/ul>\n<h2>Resources<\/h2>\n<ul>\n<li><a href=\"http:\/\/guides.rubyonrails.org\/association_basics.html\" target=\"_blank\">Active Record Associations<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Associations between models make common operations simpler and easier class Customer &lt; ActiveRecord::Base has_many :orders, dependent: :destroy end class Order &lt; ActiveRecord::Base belongs_to :customer end By declaring that one model belongs_to another, you instruct Rails to maintain Primary Key-Foreign Key information between instances of the two models, and you also get a number of utility [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[144],"tags":[152],"class_list":["post-3931","post","type-post","status-publish","format-standard","hentry","category-web-shots","tag-ruby"],"_links":{"self":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/comments?post=3931"}],"version-history":[{"count":9,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3931\/revisions"}],"predecessor-version":[{"id":3940,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3931\/revisions\/3940"}],"wp:attachment":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/media?parent=3931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/categories?post=3931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/tags?post=3931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}