简体   繁体   English

Active Record保存如何初始化模型?

[英]How does Active Record save initialize the model?

When you save a model, there are some model attributes that get their value from the database like: 保存模型时,有一些模型属性可从数据库中获取其值,例如:

  1. primary key 首要的关键
  2. column defaults set at the mysql level 列默认设置为mysql级别

When you insert a record (save), does it actually make 2 calls under the covers ie Insert and then Select ? 当您插入记录(保存)时,它实际上是否在封面下进行了2次调用,即“插入”然后选择“选择”?

ActiveRecord doesn't make a query after the save to get that information. 保存后,ActiveRecord不会进行查询以获取该信息。 My understanding of ActiveRecord is that it maintains internally a lot of information about the database structure, for example the names and types of all the columns, so that it can generate the corresponding methods in the models. 我对ActiveRecord理解是,它在内部维护着许多有关数据库结构的信息,例如所有列的名称和类型,以便它可以在模型中生成相应的方法。 Also it would know what the default value is so it can set that in the model at the same time as the db sets it. 它还知道默认值是什么,因此可以在数据库设置的同时设置默认值。

This is not completely DRY and would cause lots of problems if you bypass ActiveRecord to restructure the database, but the savings in overhead with less database touches is well worth it. 这不是完全DRY,如果绕过ActiveRecord来重组数据库,则会引起很多问题,但是值得的是,用较少的数据库操作来节省开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM