Skip to content

Instantly share code, notes, and snippets.

@bayendor
Created December 17, 2013 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayendor/7999530 to your computer and use it in GitHub Desktop.
Save bayendor/7999530 to your computer and use it in GitHub Desktop.
Always use :inverse_of to connect two models in a belongs_to/has_many relationship. When you have two models in a belongs_to/has_many relationship, you should always use the :inverse_of option to tell ActiveRecord that they're two sides of the same association. By setting this option, Rails can optimize object loading so forum and forum.posts[0]…
class Forum < ActiveRecord::Base
has_many :posts, :inverse_of => :forum
end
class Post < ActiveRecord::Base
belongs_to :forum, :inverse_of => :posts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment