[Ruby-on-rails] What does inverse_of do? What SQL does it generate?


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
end

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

Now, in the console:

irb> p = Project.new
=> #<Project id: nil, name: nil, ...>
irb> t = p.tasks.build
=> #<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.

Question

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
end

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









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
end

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

  private
  def draft?
      self.dungeon.draft
  end 
end

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

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