activerecord 如何知道执行插入或更新? [英] how does activerecord know to perform an insert or update?
问题描述
也许那里的一些 ruby 专家可以阐明 activerecord 在调用 save() 时如何知道进行插入或更新.它背后的逻辑是什么?它是否检查主键是否为空或其他内容,如果是,则插入,如果不是更新?
maybe some ruby experts out there can shed some light on how activerecord know to do an insert or update when calling save(). what is the logic behind it? does it check to see if the primary key is blank or something and if so does an insert, if not an update?
推荐答案
虽然有些人说RTFM"没问题,但我更愿意说更详细但仍然完全无用的当 Rails-3-出现并改变一切回应:
Whilst it's fine for some people to say "RTFM" I rather the more walk-through-but-still-entirely-useless-when-Rails-3-comes-out-and-changes-everything response:
它在 Rails 2.3(又名今天")中的工作原理
save
调用 create_or_update
看起来像这样:
save
calls create_or_update
which looks like this:
def create_or_update
raise ReadOnlyRecord if readonly?
result = new_record? ? create : update
result != false
end
您可以忽略此方法的第一行,因为它仅在记录是只读的情况下才会引发错误(通常不是,但在连接的情况下可能是).我们在这里感兴趣的是方法中的第二行和第三行.
You can ignore the first line of this method as it only raises an error if the record is readonly (it isn't usually, but in the case of joins it may be). What we are interested in here is the second and third lines inside the method.
第二行调用 new_record?
定义如下:
The second line calls new_record?
which is defined as this:
# Returns true if this object hasn't been saved yet -- that is, a record for the object doesn't exist yet; otherwise, returns false.
def new_record?
@new_record || false
end
当 initialize
(new
调用 initialize
,并给我们一个新的对象,这里有一些背景 Ruby-fu) 方法被调用.
And the variable @new_record
is set when the initialize
(new
calls initialize
, and gives us a new object, some background Ruby-fu here) method is called.
所以如果这个 @new_record
是 true
它会调用 create
如果它是假的,它会调用 update
我想,这让我们明白了你所追求的.
So if this @new_record
is true
it'll call create
and if it's false it'll call update
which brings us to what you're after, I think.
此外,当您找到一条记录时,它不会调用 initialize
,因此不会设置 @new_record
.如果你注意到了,new_record?
背后的代码是 @new_record ||false
,表示如果未设置 @new_record
将返回 false.
Furthermore, when you find a record it does not call initialize
and therefore does not set @new_record
. If you noticed, the code behind new_record?
was @new_record || false
, meaning it will return false if @new_record
was not set.
例如,假设您要查找最后一个 Forum
记录,那么您将执行 Forum.last
.
Let's say for example you want to find the last Forum
record, so you would do Forum.last
.
- 这调用了 Forum 类的 >
last
方法,继承自 ActiveRecord::Base last
调用find
类方法.find
调用find_last
find_last
调用find_initial
find_initial
调用find_every
find_every
调用find_by_sql
- 和<代码>find_by_sql 调用
实例化
- This calls the
last
method on theForum
class, which inherits from ActiveRecord::Base last
calls thefind
class method.find
callsfind_last
find_last
callsfind_initial
find_initial
callsfind_every
find_every
callsfind_by_sql
- and
find_by_sql
callsinstantiate
您会在此处看到此更改中没有设置 @new_record
,因此 find
获得的任何记录都不会是新记录.
You'll see here that nowhere along this change is @new_record
set and thus any record obtained by find
will not be a new record.
希望这能帮助您理解.
这篇关于activerecord 如何知道执行插入或更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!