什么是Rails ActiveRecord

Active Record is the M in MVC — the model — which is the layer of the system responsible for representing business data and logic.[Source: https://guides.rubyonrails.org/active_record_basics.html]

Active Record是MVC中的M(模型),它是负责表示业务数据和逻辑的系统层。 [来源: https //guides.rubyonrails.org/active_record_basics.html ]

Rails Active Records provide an interface and binding between the tables in a relational database and the Ruby program code that manipulates database records. Ruby method names are automatically generated from the field names of database tables.

Rails Active Records提供了关系数据库中的表与操作数据库记录的Ruby程序代码之间的接口和绑定。 Ruby方法名称是根据数据库表的字段名称自动生成的。

Each ActiveRecord object has a CRUD (Create, Read, Update, and Delete) methods for database access.

每个ActiveRecord对象都有一个用于数据库访问的CRUD(创建,读取,更新和删除)方法。

This strategy allows for simple designs and straight forward mappings between database tables and application objects.

这种策略允许简单的设计以及数据库表和应用程序对象之间的直接映射。

[Source: https://www.tutorialspoint.com/ruby-on-rails/rails-active-records.htm]

[来源: https //www.tutorialspoint.com/ruby-on-rails/rails-active-records.htm ]

When we want to run a SQL query in database server, we normally do it in few steps:

当我们要在数据库服务器中运行SQL查询时,通常需要执行几个步骤:

  1. Create a database connection.

    创建数据库连接。
  2. Write the SQL query.

    编写SQL查询。
  3. Run this SQL query in the database.

    在数据库中运行此SQL查询。
  4. And get data from the database.

    并从数据库中获取数据。

So, we need to do many tasks to run a simple database query.

因此,我们需要执行许多任务来运行简单的数据库查询。

But, ActiveRecord did most of these tasks for us. In ActiveRecord, we do not even need to write SQL queries.

但是,ActiveRecord为我们完成了大多数这些任务。 在ActiveRecord中,我们甚至不需要编写SQL查询。

Let’s see an example. Suppose, there is a User model in a Rails project, we want to get all “users” from the database. We simply write: User.all and we get all users from the database.

让我们来看一个例子。 假设在Rails项目中有一个User模型,我们想从数据库中获取所有“用户”。 我们只需编写: User .all,然后从数据库中获取所有用户。

@users = User.all@users.each do |user|puts user.nameend

可以在ActiveRecord中运行什么查询? (What query can be run in ActiveRecord?)

ActiveRecord has many methods for common tasks. Like:

ActiveRecord有许多用于常见任务的方法。 喜欢:


// find to a single item with id: User.find(5)
// Create user and save in the database:
user = User.new(name: “John”)user.save// Update existing record:user = User.find(4)user.name = “Robert”user.save// Insert in the database with single line:User.create(name: “john”)//Delete an user object:user = User.find(5)user.delete

You can even run a solid MySQL query with ActiveRecord.

您甚至可以使用ActiveRecord运行可靠MySQL查询。

您可以使用几行代码来切换到其他数据库 (You can use a switch to the different database with few lines of code)

Suppose, on your laptop, you are using MySQL database for development and when you deploy in production, you want to use the Postgresql database.

假设在笔记本电脑上,您正在使用MySQL数据库进行开发,而在生产环境中进行部署时,则要使用Postgresql数据库。

It is very easy to do it in the Rails App.

在Rails应用程序中很容易做到。

You just add pg gem in Gemfile, bundle install, and update the config/database.yml. Very simple, isn’t it?

您只需在Gemfile中添加pg gem,捆绑安装并更新config / database.yml。 很简单,不是吗?

Rails模型在保存之前执行验证 (Rails model performs validation before saving)

Rails have many validations. You just write a few lines of code in the model to add validation in an attribute.

Rails有很多验证。 您只需在模型中编写几行代码即可在属性中添加验证。

class Uservalidates :email, presence: trueend

Here you will find all validations: https://guides.rubyonrails.org/active_record_validations.html

在这里,您会找到所有验证: https : //guides.rubyonrails.org/active_record_validations.html

You can also write custom validations.

您也可以编写自定义验证。

您可以使用ActiveRecord运行迁移 (You can run migration with ActiveRecord)

If you need to update the database schema, you should do it with migration. ActiveRecord has different types of migration. such as:

如果需要更新数据库架构,则应通过迁移来完成。 ActiveRecord具有不同类型的迁移。 如:

  • create table

    创建表
  • drop table

    放下桌子
  • add column

    添加列
  • change column

    变更栏
  • remove column

    删除列
  • and many more….

    还有很多…。

You can check the official documentation here: https://guides.rubyonrails.org/active_record_migrations.html

您可以在此处查看官方文档: https : //guides.rubyonrails.org/active_record_migrations.html

摘要: (Summary:)

ActiveRecord is by default installed in the new Rails App. Rails developers love ActiveRecord. It saves time, enhances security, and using ActiveRecord is really easy and fun. Happy coding.

默认情况下,ActiveRecord已安装在新的Rails应用程序中。 Rails开发人员喜欢ActiveRecord。 它可以节省时间,增强安全性,并且使用ActiveRecord确实非常容易和有趣。 快乐的编码。

翻译自: https://medium.com/@moulayjam/what-is-rails-activerecord-2a891390703b

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值