ga-wdi-boston / rails-api-many-to-many

Placeholder. Code in the rails-api-*. demo/code-along/lab series
Other
1 stars 77 forks source link

Remove `inverse_of`? Or show a better use-case? #14

Closed payne-chris-r closed 7 years ago

payne-chris-r commented 7 years ago

Warning, recent changes in both rails 4 and rails 5 have made previous best practices regarding inverse_of potentially obsolete (thought they're still perfectly safe to set as long as you don't misspell anything!)

Finn: As an example, the relationships in the above article

As of version 4.1, Rails will try to automatically set the :inverse_of option for you. Given our example with Criminal having a :belongs_to association with Prison, it will attempt to derive the inverse from the class name -- in this case :criminals based on the class Criminal. Obviously, this falls apart when the names do not match up. For example, when using :class_name or :foreign_key options on your associations. In that case, :inverse_of has to be explicitly set to the correct names, so it's still worth knowing how :inverse_of works.

@channel :point_up: does this mean we should be doing the following:

  has_many :primary_care_recipients, class_name: 'Patient',
                                     inverse_of: :primary_care_physician
  has_many :patients, through: :appointments
end

???

MicFin commented 7 years ago

That looks like an accurate example to me.
With that set up, the Doctor will have many appointments and many patients through those appointments which is the many to many relationship to Patients.
The Doctor will also have many primary_care_recipients which is the one to many relationship to Patients.

MicFin commented 7 years ago

Closing per explanation added to README https://github.com/ga-wdi-boston/rails-api-many-to-many/commit/34df18dfeccf19f1781783cd1765d04682009dbe