reload

whats the use for reload? and how it works ?

Class reloading, if enabled, is triggered by some code at the end of each request cycle.

In the console, however, there are no requests going on so code changes are not refreshed. The reload! method is offered as a way to refresh classes manually.

-- fxn

prabu wrote:

whats the use for reload? and how it works ?

Try this test case:

    assert_difference @tygr.props, :count, -1 do
      xhr :get, 'apply_prop', :prop_id => potion.id, :user_id => @tygr.id
      @tygr.reload
    end

Before calling xhr :get, the @tygr model object has one prop. xhr :get
tests the 'apply_prop' action, which must load its own copy of the
same tygr model object, to consume its prop and save the new list.

So, before assert_difference detects the prop count increases by -1,
the @tygr object gets a reload to refresh its prop list.

Maybe @tygr.props.reload would be cleaner. And if my tests have too
many reloads, I should bottle them up, like this:

  def assert_model_difference(model, method, difference)
    assert_difference model, method, difference do
      yield
      model.reload
    end
  end

...

    assert_model_difference @tygr.props, :count, -1 do
      xhr :get, 'apply_prop', :prop_id => potion.id, :user_id => @tygr.id
    end

Those are all test-side reloads, because the testing code often cannot
pass the same model object into the production code as it will use.
sessions and params should only pass IDs. So we get an "alias"
situation, with two in-memory copies of the same model object
identity, and we need save on one side and reload on the other to
synchronize them.

In production code, I suspect that too many 'reload' calls are a
"design smell" - a hint that something else in the design should
improve. An event-driven program should not alias the same model
objects all over the place; it should load them up once, mess with
them once, and save them once.

I will crack down on y'all abusing that guideline as soon as I figure
out how to follow it myself!