The rails method :inverse_of can be used to build an in-memory association between objects, so that you can reduce queries in a database: http://mediumexposure.com/better-memory-associations-inverseof/
What are the costs associated with inverse_of? Why is it that we don't write inverse_of on every relationship that exists, to reduce queries?
They essentially do the same thing, the only difference is what side of the relationship you are on. If a User has a Profile , then in the User class you'd have has_one :profile and in the Profile class you'd have belongs_to :user . To determine who "has" the other object, look at where the foreign key is.
Polymorphic relationship in Rails refers to a type of Active Record association. This concept is used to attach a model to another model that can be of a different type by only having to define one association.
Self-referential association means we create a JOIN MODEL, such as Friendship, for example, which links another model, such as User to itself, so a user can have many friends (which are other users), and a friend can be befriended by a user ( a follower and a followed).
Stories can belong to many categories. Categories can have many stories. has_many :through gives you a third model which can be used to store various other pieces of information which don't belong to either of the original models. Person can subscribe to many magazines.
From Ernie Miller's talk at Railsconf 2013 (around 15:30)
[...] inverse_of is the oldest ActiveRecord feature you aren't using, and you totally should. [...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With