Forum: Ruby on Rails reload

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
prabu (Guest)
on 2007-03-05 12:39
(Received via mailing list)
whats the use for reload? and how it works ?
Xavier Noria (Guest)
on 2007-03-05 12:49
(Received via mailing list)
On Mar 5, 2007, at 12:37 PM, prabu wrote:

> 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
Phlip (Guest)
on 2007-03-05 15:52
(Received via mailing list)
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!

--
  Phlip
  http://c2.com/cgi/wiki?ZeekLand  <-- NOT a blog!!
This topic is locked and can not be replied to.