Beta đầu tiên của Rails 8 chính thức được release, mang来一 bộ tính năng, bug fixes và cải tiến令人 hứng thú. Phiên bản này xây dựng trên nền tảng của Rails 7.2, đồng thời giới thiệu tính năng mới và optimizations để làm cho Rails development thậm chí còn productive và enjoyable hơn.
Key highlights bao gồm tích hợp với Kamal 2 cho deployments hassle-free, giới thiệu Propshaft làm asset pipeline mặc định mới, và extensive ActiveRecord enhancements. Rails 8 cũng mang lại một vài SQLite integration upgrades khiến nó trở thành lựa chọn viable cho production use.
Hãy dive in và explore mọi thứ Rails 8 cung cấp!
Deployments Effortless với Kamal 2 và Thruster
Rails 8 làm cho deploy ứng dụng trở nên simple với Kamal 2 và Thruster.
Kamal 2 giảm sự phụ thuộc vào managed cloud services và Platform as a Service (PaaS) bằng cách enable quick và easy deployment lên cloud VMs, bare metal servers, hoặc VPS environments chỉ trong vài phút.
Với một command đơn giản (kamal setup), bạn có thể setup production-ready Rails environment trên một standard Linux box, làm cho deployment vừa easy vừa cost-effective.
Kamal 2 cũng tích hợp với Thruster, một custom proxy built specifically cho Rails cho phép zero-downtime deployments, HTTP/2 support, automated SSL với Let's Encrypt, Gzip compression, và easy hosting multiple apps trên single server — tất cả không cần complex setup.
Với Kamal 2 và Thruster, Rails 8 làm cho deploy apps dễ dàng hơn bao giờ hết. Và nếu bạn thích deployment setup khác, bạn có thể opt out dùng flag --skip-kamal để maintain existing workflows.
Leaner Rails Deployments với Solid Adapters
Một trong những cải tiến lớn trong Rails 8 là simpler deployments bằng cách giảm số additional services cần thiết để implement common web application requirements.
Truyền thống, nếu bạn cần features như job queues, caching, và pub/sub messaging, bạn sẽ dùng combination của database như PostgreSQL với Redis cho auxiliary functions.
Với Rails 8, bạn có thể handle tất cả những thứ đó chỉ với SQLite, nhờ ba database-backed adapters mới: Solid Cable, Solid Cache, và Solid Queue.
-
Solid Cable là Rails' mới default Action Cable adapter trong production và có nghĩa là bạn có thể drop dependency phổ biến trên Redis. Nó đóng vai pub/sub server, relay messages giữa app và connected clients dùng fast polling qua SQLite. Dù dùng polling, performance của Solid Cable comparable với Redis trong most situations.
-
Solid Cache thay thế cần Redis bằng cách dùng disk storage thay vì RAM cho caching. Approach này cho phép caches lớn hơn, cost-effective hơn, persist lâu hơn và handle nhiều requests hơn mà không hy sinh performance. Nó cũng hỗ trợ encrypted storage và retention policies để meet privacy requirements.
-
Solid Queue thay thế Redis cho Active Job background processing, dùng cơ chế
FOR UPDATE SKIP LOCKEDcho efficient job handling (compatible với PostgreSQL, MySQL, hoặc SQLite). Nó bao gồm essential features như concurrency control, retries, và recurring jobs, và đã chứng minh mình tại HEY, nơi nó manage 20 triệu jobs mỗi ngày.
Ba adapters này được thiết kế quanh một ý tưởng đơn giản: modern SSDs và NVMe drives đủ nhanh để handle nhiều tasks trước đây cần in-memory solutions. Bằng cách tap vào speedy drives này, Rails cắt bỏ need cho separate RAM-based tools như Redis.
SQLite Sẵn sàng cho Production
Rails 8 đưa SQLite từ lightweight development tool thành reliable choice cho production use, nhờ extensive work trên SQLite adapter và Ruby driver.
Với giới thiệu solid adapters thảo luận ở trên, SQLite giờ có capability power Action Cable, Rails.cache, và Active Job effectively, mở rộng role của nó beyond just prototyping hoặc testing environments.
Dưới đây là một số key improvements cho SQLite integration trong Rails 8:
- Full-text search và virtual tables giờ supported dùng
create_virtual_table. - Adapter giờ cho phép bulk insert fixtures cho enhanced data seeding performance.
- Transactions default to
IMMEDIATEmode để improve concurrency. - Enhanced error handling bằng cách translate
SQLite3::BusyExceptionthànhActiveRecord::StatementTimeout.
New Era cho Asset Pipeline với Propshaft
Rails 8 cũng giới thiệu Propshaft làm asset pipeline default mới, thay thế Sprockets system lâu đời. Sprockets served Rails developers tốt hơn một thập kỷ, nhưng nó được thiết kế trong era khác — trước sự bùng nổ của JavaScript build tools và modern browser improvements.
Propshaft phản ánh simpler, modern approach đến managing assets, built around core needs của today's developers. Mục đích của nó straightforward: cung cấp clear path cho assets và apply digest stamps cho caching.
Khác với Sprockets, đảm nhận nhiều additional tasks, Propshaft focus chỉ vào what's essential, fitting naturally với Rails philosophy mới của keeping asset pipelines lean (trong khi complex JavaScript handling để cho specialized tools như Esbuild hoặc Vite).
Built-In Authentication Made Simple
Rails đã build key components của authentication qua nhiều năm, từ has_secure_password trong Rails 5 đến normalizes, generates_token_for và authenticate_by trong Rails 7.1.
Với Rails 8, tất cả components này come together để give bạn straightforward starting point để build secure, session-based authentication system.
Bằng cách chạy một command đơn giản, bạn có thể setup tất cả essentials cho authentication system với database-backed sessions và password reset functionality:
Command này generate key files, bao gồm models, controllers, mailers, và views:
Điều này effectively puts bạn on fast track đến secure, production-ready authentication. Tất cả còn lại là integrate user sign-up flow customized cho needs của ứng dụng.
New Script Folder và Generator
Rails 8 giới thiệu script folder mới dedicated cho holding one-off hoặc general-purpose scripts, như data migrations, cleanup tasks, hoặc utility operations khác. Addition này giúp organize scripts gọn gàng, tách biệt khỏi main application logic.
Để làm script creation dễ dàng hơn, một script generator mới available. Bạn có thể generate scripts với command đơn giản:
Các command này tạo corresponding script files, mà bạn có thể execute với:
Streamlined approach này giữ ứng dụng organized và làm cho handling custom scripts convenient và maintainable hơn.
A Slew of Active Record Improvements
Active Record cũng thấy major enhancements trong Rails 8 để improve performance, simplify migrations, improve troubleshooting, và provide better support cho complex database use cases.
Dưới đây là một số key changes introduced trong latest version này:
- Rails 8 giờ distinguish giữa
float4vàfloat8trong PostgreSQL. drop_tablegiờ support dropping multiple tables at once.- Support advanced options khi create table với PostgreSQL, bao gồm inheritance và partitioning.
- Bulk inserts của fixtures giờ supported để improve data seeding performance.
- Migrate fresh database giờ bắt đầu bằng load database schema trước khi run migrations.
create_schemavàdrop_schemaoperations giờ reversible.- Rails 8 giờ require MySQL 5.6.4 hoặc mới hơn do advancements như datetime with precision.
- Query log tags enabled by default trong development environments để trace SQL statements back to application code và identify database nào in use.
Kết thúc
Rails 8 giới thiệu một range impactful updates, từ easier deployments với Kamal và modern asset pipeline đến significant ActiveRecord enhancements và improved production capabilities cho SQLite.
Những advancements này không chỉ boost developer productivity mà cũng align với modern best practices, cho phép bạn focus vào building application thay vì deal với infrastructure complexities.
Cho detailed list của all new features, optimizations, và changes, check out official Rails 8 release notes.
Nếu bạn muốn get involved với contributing to Rails, visit Rails GitHub repository để explore open issues và review contribution guidelines.
Thanks for reading!
P.S. Nếu bạn muốn đọc Ruby Magic posts ngay khi off the press, subscribe Ruby Magic newsletter và never miss a single post!
Damilola Olatunji
Damilola là freelance technical writer và software developer based in Lagos, Nigeria. Anh chuyên về JavaScript và Node.js, và aims to deliver concise và practical articles cho developers. Khi không viết hoặc coding, anh enjoy reading, playing games, và traveling.
Tất cả bài viết của Damilola Olatunji