Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there costs associated with inverse_of? Should we add it to relationships wherever possible?

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?

like image 774
jay Avatar asked Nov 13 '12 08:11

jay


People also ask

How would you choose between Belongs_to and Has_one?

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.

What is Rails polymorphic association?

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.

What is self association in Rails?

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).

What has and belongs to many or has many through?

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.


1 Answers

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. [...]

like image 112
Ruy Diaz Avatar answered Oct 12 '22 21:10

Ruy Diaz