ruby-on-rails rails polymorphic - What does inverse_of do? What SQL does it generate?

4 Answers

I think :inverse_of is most useful when you are working with associations that have not yet been persisted. E.g.:

class Project < ActiveRecord::Base
  has_many :tasks, :inverse_of=>:project

class Task < ActiveRecord::Base
  belongs_to :project, :inverse_of=>:tasks

Now, in the console:

irb> p =
=> #<Project id: nil, name: nil, ...>
irb> t =
=> #<Task id: nil, project_id: nil, ...>
irb> t.project
=> #<Project id: nil, name: nil, ...>

Without the :inverse_of arguments, t.project would return nil, because it triggers an sql query and the data isn't stored yet. With the :inverse_of arguments, the data is retrieved from memory.

has_one false multiple

I'm trying to get my head around inverse_of and I do not get it.

What does the generated sql look like, if any?

Does the inverse_of option exhibit the same behavior if used with :has_many, :belongs_to, and :has_many_and_belongs_to?

Sorry if this is such a basic question.

I saw this example:

class Player < ActiveRecord::Base
  has_many :cards, :inverse_of => :player

class Card < ActiveRecord::Base
  belongs_to :player, :inverse_of => :cards

Just an update for everyone - we just used inverse_of with one of our apps with a has_many :through association

It basically makes the "origin" object available to the "child" object

So if you're using the Rails' example:

class Dungeon < ActiveRecord::Base
  has_many :traps, :inverse_of => :dungeon
  has_one :evil_wizard, :inverse_of => :dungeon

class Trap < ActiveRecord::Base
  belongs_to :dungeon, :inverse_of => :traps
  validates :id,
      :presence => { :message => "Dungeon ID Required", :unless => :draft? }

  def draft?

class EvilWizard < ActiveRecord::Base
  belongs_to :dungeon, :inverse_of => :evil_wizard

Using :inverse_of will allow you to access the data object that it's the inverse of, without performing any further SQL queries