在ruby控制台中,如何更新实例变量?

时间:2021-03-10 23:34:02

I have a model named "Person". In ruby console, I first declare an instance of Person, then I update the attributes, then save.

我有一个名为“人”的模型。在ruby控制台中,我首先声明Person的实例,然后更新属性,然后保存。

person = Person.last
person.name = "jeff"
person.save

After doing these, I got message like this:

完成这些后,我得到这样的消息:

   (9.9ms)  BEGIN
   (7.5ms)  ROLLBACK
=> false

What are "BEGIN", "ROLLBACK", "false" refers to separately? I googled, but nothing came out.

什么是“BEGIN”,“ROLLBACK”,“false”分别指?我用Google搜索,但什么也没出来。

2 个解决方案

#1


When you do save in Rails, it wraps the database operation in a transaction. BEGIN is written to the log when the transaction starts and ROLLBACK is logged if the operation fails (because all write operations in the transaction—UPDATE, INSERT or DELETE—are "rolled back").

当您在Rails中保存时,它会在事务中包装数据库操作。事务开始时将BEGIN写入日志,如果操作失败则记录ROLLBACK(因为事务UPDATE,INSERT或DELETE中的所有写操作都被“回滚”)。

false is the value that save returns when the operation fails.

false是操作失败时save返回的值。

You usually want to use save! instead of save because it will raise an (informative) exception if the operation fails.

你通常想用保存!而不是保存,因为如果操作失败,它将引发(信息性)异常。

#2


Also look at update_attributes:

另请参阅update_attributes:

http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html

#1


When you do save in Rails, it wraps the database operation in a transaction. BEGIN is written to the log when the transaction starts and ROLLBACK is logged if the operation fails (because all write operations in the transaction—UPDATE, INSERT or DELETE—are "rolled back").

当您在Rails中保存时,它会在事务中包装数据库操作。事务开始时将BEGIN写入日志,如果操作失败则记录ROLLBACK(因为事务UPDATE,INSERT或DELETE中的所有写操作都被“回滚”)。

false is the value that save returns when the operation fails.

false是操作失败时save返回的值。

You usually want to use save! instead of save because it will raise an (informative) exception if the operation fails.

你通常想用保存!而不是保存,因为如果操作失败,它将引发(信息性)异常。

#2


Also look at update_attributes:

另请参阅update_attributes:

http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html