Ruby-on-rails 我怎么知道什么时候在 Rails 中“刷新”我的模型对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5519741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I know when to "refresh" my model object in Rails?
提问by Geo
Here's part of an integration test that I'm having:
这是我正在进行的集成测试的一部分:
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 wasn't reinitializing userafter confirm_email_user_url was accessed, and the state was always inactiveeven though the user was activated.
我花了最后一个小时来调试这个:)。在我的初始版本中,user在访问 confirm_email_user_url 后我没有重新初始化,inactive即使用户被激活,状态也始终保持不变。
How do I know if I should "reload" ( lacking of a better name ) my model object? What should I call in order to do so?
我怎么知道我是否应该“重新加载”(缺少更好的名称)我的模型对象?我应该打电话给什么?
回答by dantswain
You'd need to call user.reloadwhenever the data has changed in the database.
user.reload每当数据库中的数据发生更改时,您都需要调用。
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_urlmodifies the database. The object doesn't know about this until you reloadit, which re-acquires the data from the database.
在上面的代码中,“用户”对象是在内存中从数据库中获取的数据创建的User.first。然后,看起来您confirm_email_user_url修改了数据库。对象不知道这一点,直到您reload从数据库中重新获取数据。
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 = "[email protected]"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.
我不确定是否有一种编程方式可以知道何时需要重新加载对象,但作为开发人员,您应该了解正在发生的事情并进行适当的处理。根据我的大部分经验(有些有限),这只是测试期间的问题。在生产中,对象加载到内存中时在数据库中修改对象的情况并不常见。通常发生的事情是修改内存中的对象,然后将其保存到数据库中(即,user.email = "[email protected]"后跟user.save)。我想如果您有一个高活动应用程序,其中许多用户可能会在短时间内连续修改某些内容,那么您需要小心处理。
回答by Thomas Fankhauser
Btw. this doesn't really work when you do stuff on the model itself like Report.count. After endless tries of resetting the column information or getting an instance of the first/last record and reload it the only thing that helped me was reconnecting the database between the counts like this:
顺便提一句。当你在模型本身上做一些事情时,这真的不起作用Report.count。在无数次尝试重置列信息或获取第一个/最后一个记录的实例并重新加载它之后,唯一帮助我的是在这样的计数之间重新连接数据库:
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 the 3+ versions.
这适用于 Rails 2.3.8+,我不知道 3+ 版本。

