How to Know When to "Refresh" My Model Object in Rails

How can I know when to refresh my model object in Rails?

You'd need to call user.reload whenever the data has changed in the database.

In your above code, the "user" object is created in memory from the data fetched from the database by User.first. Then, it looks like your confirm_email_user_url modifies the database. The object doesn't know about this until you reload it, which re-acquires the data from the database.

I'm not sure if there's a programmatic way to know when you will need to reload the object, but as a developer you should be aware of what is going on and handle appropriately. In most of my experience (which is somewhat limited), this is only an issue during testing. In production, it's not typical for an object to be modified in the database while it is loaded in memory. What usually happens is the object in memory is modified and then saved to the database (i.e., user.email = "foo@bar.com" followed by user.save). I suppose if you had a high-activity application where lots of users might be modifying something in short succession, you would want to be careful about it.

Reload a model attribute

Beats me why you'd want to do such a thing, but:

def reload_attribute(attr)
value = self.class.where(:id=>id).select(attr).first[attr]
self[attr] = value
end

This issues a SELECT that retrieves just the one column and assigns its value to the attribute in the current model instance. Call with (eg):

post = Post.find(1)
post.reload_attribute(:body)

Really though, you should just go with post.reload (doc). Unless you have hugely wide columns that impose some performance cost on doing SELECT *

how do I reload a model in rails console?

reload! doesn't reinitialize existing objects, it just reloads the code. If you create a new instance of the class it should reflect the changes.

How to determine if a record is just created or updated in after_save

I was looking to use this for an after_save callback.

A simpler solution is to use id_changed? (since it won't change on update) or even created_at_changed? if timestamp columns are present.

Update: As @mitsy points out, if this check is needed outside of callbacks then use id_previously_changed?. See docs.

Why use 'reload' method after saving object? (Hartl Rails Tut 6.30)

Yes in this case @user.reload.email and @user.email is the same thing. But it's good practice to use @user.reload.email instead of @user.email to check what is exactly saved in the database i mean you don't know if you or someone add some code in after_save which changes it's value then it will not have effect on your tests.

EDIT:
And also what you are checking is what's saved in the database so @user.reload.email exactly reflects what's saved in database then @user.email

Serialized column by model in rails work correctly only after refresh

Deyamlizing an object of class Foo will do funny things if there is no class Foo. This can quite easily happen in development becauses classes are only loaded when needed and unloaded when rails thinks they might have changed.

Depending on whether the class is loaded or not the YAML load will have different results (YAML doesn't know about rail's automatic loading stuff)

One solution worth considering is to store the attributes hash rather than the activerecord object. You'll probably avoid problems in the long run and it will be more space efficient in the long wrong - there's a bunch of state in an activerecord object that you probably don't care about in this case.

If that's not an option, your best bet is probably to make sure that the classes that the serialized column might contain are loaded - still a few calls to require_dependency 'foo' at the top of the file.

Rails - How to refresh an association after a save

You can use item.reload that will refetch the model from the database and next time you call an association, it will refetch it.

Rails Console: reload! not reflecting changes in model files? What could be possible reason?

reload! only reloads the latest code in the console environment. It does not re-initialize existing objects.

This means if you have already instantiated any objects, their attributes would not be updated - including newly introduced validations. However, if you create a new object, its attributes (and also validations) will reflect the reloaded code.
more here



Related Topics



Leave a reply



Submit