您的位置:首页 > 科技 > IT业 > iosapp开发_网络设计的目的是什么_网推项目接单平台_推广平台

iosapp开发_网络设计的目的是什么_网推项目接单平台_推广平台

2024/12/23 3:03:52 来源:https://blog.csdn.net/Blue___Ocean/article/details/144216415  浏览:    关键词:iosapp开发_网络设计的目的是什么_网推项目接单平台_推广平台
iosapp开发_网络设计的目的是什么_网推项目接单平台_推广平台

Active Record Basics — Ruby on Rails Guides

Active Record Migrations — Ruby on Rails Guides

原文链接自取

1.Active Record是什么?

Active Record是MVC模式中M的一部分,是负责展示数据和业务逻辑的一层,可以帮助你创建和使用Ruby对象,这些对象的属性需要持久存储到数据库中。

Active Record 有一些约定好的规则,它在不需要配置的情况下使用这些规则映射models和数据表:Rails 会将你model的类名复数化用来寻找对应的数据表,比如一个类名为Book将会被隐射到数据表books; BookClub的类将被映射到book_clubs.

2.Active Record Models?

创建过Models的人都经常能看见这个,估计也不明白这个具体是啥,只知道它将创建一个Book model,映射到数据库中的books table, 表中的每一栏都可以映射到Book类中的属性。An instance of Book can represent a row in the books table.

class Book < ApplicationRecord
end

官网解答来了

When generating a Rails application, an abstract ApplicationRecord class will be created in app/models/application_record.rb. The ApplicationRecord class inherits from ActiveRecord::Base and it's what turns a regular Ruby class into an Active Record model.

ApplicationRecord is the base class for all Active Record models in your app. To create a new model, subclass the ApplicationRecord class.

因为我对此了解也不深入,所以就不翻译了,从原文里可以了解的更多。

3.Create, Read, Update, and Delete(CRUD)

Active Record automatically creates methods to allow you to read and manipulate data stored in your application's database tables.

Active Record通过抽象出数据库访问细节的高级方法,无缝执行DRUD操作。注意,所有这些方便的方法都会产生针对底层数据库执行的SQL语句。

下面的示例展示了一些 CRUD 方法以及由此产生的 SQL 语句。

3.1 Create

Active Record对象可以通过哈希值,块创建,也可以在创建后手动设置属性。new 方法将返回一个新的,非存在的对象,而create将把这个对象保存到数据库并返回。

例如,给定一个具有title和author属性的Book model,调用create方法将创建一个对象并保存一条新纪录到数据库中。

book = Book.create(title: "The Lord of the Rings", author: "J.R.R. Tolkien")# Note that the `id` is assigned as this record is committed to the database.
book.inspect
# => "#<Book id: 106, title: \"The Lord of the Rings\", author: \"J.R.R. Tolkien\", created_at: \"2024-03-04 19:15:58.033967000 +0000\", updated_at: \"2024-03-04 19:15:58.033967000 +0000\">"
book = Book.new
book.title = "The Hobbit"
book.author = "J.R.R. Tolkien"# Note that the `id` is not set for this object.
book.inspect
# => "#<Book id: nil, title: \"The Hobbit\", author: \"J.R.R. Tolkien\", created_at: nil, updated_at: nil>"# The above `book` is not yet saved to the database.book.save
book.id # => 107# Now the `book` record is committed to the database and has an `id`.

book.save 和 Book.create 产生的 SQL 语句如下所示:

/* Note that `created_at` and `updated_at` are automatically set. */INSERT INTO "books" ("title", "author", "created_at", "updated_at") VALUES (?, ?, ?, ?) RETURNING "id"  [["title", "Metaprogramming Ruby 2"], ["author", "Paolo Perrotta"], ["created_at", "2024-02-22 20:01:18.469952"], ["updated_at", "2024-02-22 20:01:18.469952"]]

3.2 Read

# Return a collection with all books.
books = Book.all# Return a single book.
first_book = Book.first
last_book = Book.last
book = Book.take

对应的SQL:

-- Book.all
SELECT "books".* FROM "books"-- Book.first
SELECT "books".* FROM "books" ORDER BY "books"."id" ASC LIMIT ?  [["LIMIT", 1]]-- Book.last
SELECT "books".* FROM "books" ORDER BY "books"."id" DESC LIMIT ?  [["LIMIT", 1]]-- Book.take
SELECT "books".* FROM "books" LIMIT ?  [["LIMIT", 1]]

这个地方有个需要注意的点,就是find_by和where,他们都可以用来查找特定的图书。find_by返回单条记录,而where返回记录列表!

# Returns the first book with a given title or `nil` if no book is found.
book = Book.find_by(title: "Metaprogramming Ruby 2")# Alternative to Book.find_by(id: 42). Will throw an exception if no matching book is found.
book = Book.find(42)

对应的SQL

-- Book.find_by(title: "Metaprogramming Ruby 2")
SELECT "books".* FROM "books" WHERE "books"."title" = ? LIMIT ?  [["title", "Metaprogramming Ruby 2"], ["LIMIT", 1]]-- Book.find(42)
SELECT "books".* FROM "books" WHERE "books"."id" = ? LIMIT ?  [["id", 42], ["LIMIT", 1]]
# Find all books by a given author, sort by created_at in reverse chronological order.
Book.where(author: "Douglas Adams").order(created_at: :desc)
SELECT "books".* FROM "books" WHERE "books"."author" = ? ORDER BY "books"."created_at" DESC [["author", "Douglas Adams"]]

读取查询记录的方法还有很多。Active Record Query Interface — Ruby on Rails Guides

3.3 Update

检索到 Active Record 对象后,就可以修改其属性并保存到数据库中

book = Book.find_by(title: "The Lord of the Rings")
book.title = "The Lord of the Rings: The Fellowship of the Ring"
book.save

可以使用hash简写

book = Book.find_by(title: "The Lord of the Rings")
book.update(title: "The Lord of the Rings: The Fellowship of the Ring")

对应的SQL

/* Note that `updated_at` is automatically set. */UPDATE "books" SET "title" = ?, "updated_at" = ? WHERE "books"."id" = ?  [["title", "The Lord of the Rings: The Fellowship of the Ring"], ["updated_at", "2024-02-22 20:51:13.487064"], ["id", 104]]

如果想批量更新多条记录 without callbacks or validations,可以直接使用update_all

Book.update_all(status: "already own")

3.4 Delete

同样,检索到Active Record 对象后,也可以删除它,并从数据库中删除。

book = Book.find_by(title: "The Lord of the Rings")
book.destroy

对应的SQL

DELETE FROM "books" WHERE "books"."id" = ?  [["id", 104]]

如果想批量删掉多条数据,可以使用 destroy_by 或 destroy_all 方法,注意使用这个方法蕴含的风险哈!

# Find and delete all books by Douglas Adams.
Book.destroy_by(author: "Douglas Adams")# Delete all books.
Book.destroy_all
4.Validations

Active Record允许你在model写入数据库之前验证其状态。有几种方法可以进行不同类型的验证。例如,验证属性值是否为空、是否唯一、是否已存在于数据库中、是否遵循特定格式等等。

save, create and update方法在将model持久化到数据库之前进行验证。当model无效时,这些方法返回false并且不执行数据库操作。所有这些方法都有一个对应的方法(即save!, create! and update!),它们在验证失败时会引发一个 ActiveRecord::RecordInvalid 异常,更加严格。例如:

class User < ApplicationRecordvalidates :name, presence: true
end
irb> user = User.new
irb> user.save
=> false
irb> user.save!
ActiveRecord::RecordInvalid: Validation failed: Name can't be blank

关于验证的更多内容可以看 Active Record Validations — Ruby on Rails Guides

5. Callbacks

Active Record callbacks允许你附加代码到models生命周期中的某些事件上。This enables you to add behavior to your models by executing code when those events occur, like when you create a new record, update it, destroy it, and so on.。

class User < ApplicationRecordafter_create :log_new_userprivatedef log_new_userputs "A new user was registered"end
end
irb> @user = User.create
A new user was registered

更多可见Active Record Callbacks — Ruby on Rails Guides

6.Migrations

Rails provides a convenient way to manage changes to a database schema via migrations. Migrations are written in a domain-specific language and stored in files which are executed against any database that Active Record supports.

下面的migration创建了一个名为publications 的新表:

class CreatePublications < ActiveRecord::Migration[7.2]def changecreate_table :publications do |t|t.string :titlet.text :descriptiont.references :publication_typet.references :publisher, polymorphic: truet.boolean :single_issuet.timestampsendend
end

Rails 会记录哪些migrations已提交到数据库,并将它们存储在同一数据库中名为 schema_migrations 的neighboring table中。

要运行migration 并创建表格,需要运行 bin/rails db:migrate,要回滚并删除表格,需要运行 bin/rails db:rollback。

更多请看Active Record Migrations — Ruby on Rails Guides

6.Associations

Active Record associations 允许你定义models之间的关系。Associations 可用于描述one-to-one, one-to-many, 和 many-to-many关系。 比如,“Author has many Books”的关系可以这样定义:

class Author < ApplicationRecordhas_many :books
end

Author类现在有了为an author添加和删除books的方法,甚至更多。

更多可见 Active Record Associations — Ruby on Rails Guides

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com