How do I know when to β€œupdate” my model object in Rails?

Here is part of the integration test that I have:

user = User.first assert !user.is_active? get confirm_email_user_url(user),:confirmId => user.mail_confirmation_hash assert_equal response.status,200 # because confirm_email_user_url modifies the activation state of the object user = User.first assert_equal user.state,"activated" 

I spent the last hour debugging this :). In my initial version, I did not reinitialize the user after the confirm_email_user_url file was reached, and the state is always inactive , even if the user was activated.

How do I know if I should "reload" (without having a better name) my model object? What should I name to do this?

+53
ruby ruby-on-rails activerecord testing
Apr 01 2018-11-11T00:
source share
2 answers

You will need to call user.reload whenever the data has been changed in the database.

In the above code, a user object is created in memory from data retrieved from the database using User.first . Then it looks like your confirm_email_user_url modifies the database. The object does not know about this until you reload it, which re-acquires the data from the database.

I'm not sure if there is a programmatic way to find out when you will need to reload the object, but as a developer you should know what happens and handle it accordingly. In most of my experiments (which are somewhat limited) this is only a problem during testing. In production, this is not typical of an object that needs to be modified in the database while it is loaded into memory. It usually happens that an object in memory is modified and then stored in the database (ie user.email = "foo@bar.com" and then user.save ). I suppose if you had an application with high activity in which many users could change something in a short time, you would like to be careful.

+108
Sep 16 2018-11-21T00:
source share

Btw. it really doesn’t work when you do something on the model itself, for example Report.count . After endless attempts to reset the column information or get an instance of the first / last record and reload it, the only thing that helped me was reconnecting the database between these values:

 initial_count = Report.count # do something, like invoking a rake task that imports the reports, .. Report.connection.reconnect! final_count = Report.count 

This worked for Rails 2.3.8+, I don't know about 3+ versions.

+5
Sep 19 '12 at 12:35
source share



All Articles