David Kennell
David Kennell

Reputation: 1237

What does force_reload mean?

In ActiveRecord, when an object is given a belongs_to class method, it gains access to the #other instance method, where other is the classname that was passed to belongs_to. In the documentation at http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html, though, this method is listed as other(force_reload=false). What is the force_reload option, and what happens if I set it to true?

Upvotes: 1

Views: 324

Answers (1)

Thanh
Thanh

Reputation: 8624

It's option to force association to reload.

Example, you have two model User and Address:

class User < ActiveRecord::Base
  has_one :address
end

class Address < ActiveRecord::Base
  belongs_to :user
end

And you have an address: address = Address.first

Instead of this:

user = address.user
user.reload

You can use:

user = address.user(force_reload: true)

It will fetch data of user from database (which makes an SQL query), not in memory.

Upvotes: 1

Related Questions