Question: What does force_reload mean?

Question

What does force_reload mean?

Answers 1
Added at 2016-12-23 21:12
Tags
Question

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?

Answers to

What does force_reload mean?

nr: #1 dodano: 2016-12-25 09:12

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.

Source Show
◀ Wstecz