怎么做网站渗透,广州网站建设集团,外贸机械网站,天津网站建设公司推荐Active Record Basics — Ruby on Rails Guides
Active Record Migrations — Ruby on Rails Guides
原文链接自取
1.Active Record是什么#xff1f;
Active Record是MVC模式中M的一部分#xff0c;是负责展示数据和业务逻辑的一层#xff0c;可以帮助你创建和使用Ruby…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 its 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 applications 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_all4.Validations
Active Record允许你在model写入数据库之前验证其状态。有几种方法可以进行不同类型的验证。例如验证属性值是否为空、是否唯一、是否已存在于数据库中、是否遵循特定格式等等。
save, create and update方法在将model持久化到数据库之前进行验证。当model无效时这些方法返回false并且不执行数据库操作。所有这些方法都有一个对应的方法即save!, create! and update!它们在验证失败时会引发一个 ActiveRecord::RecordInvalid 异常更加严格。例如
class User ApplicationRecordvalidates :name, presence: true
endirb user User.new
irb user.savefalse
irb user.save!
ActiveRecord::RecordInvalid: Validation failed: Name cant 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 registeredend
endirb 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
endRails 会记录哪些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
endAuthor类现在有了为an author添加和删除books的方法,甚至更多。
更多可见 Active Record Associations — Ruby on Rails Guides