[Rails Tips] Rails 7.1 – ActiveRecord::QueryMethods#select has added support for hash values

Trong bản cập nhật mới nhất của, Rails 7.1 đã cho phép chúng ta query select theo giá trị hash chứ không cần phải sử dụng raw SQL nữa.

💎 Improvements:
1️⃣ New support query với giá trị Hash
2️⃣ Chúng ta không cần phải dùng truy vấn raw version nữa

💎 Bonus:
1️⃣ Cú pháp mới cũng support chúng ta sử dụng alias
2️⃣ Sử dụng tương tự cho ActiveRecord#reselect

Ruby
# Before Rails 7.1

Post.joins(:comments)
    .select(
      "posts.id as post_id, posts.title as post_title,
      comments.id as comment_id, comments.body as comment_body"
    )
    
Post.joins(:comments).select(:id, :title, "comments.body")

# After Rails 7.1

Post.joins(:comments)
    .select(
      posts: { id: :post_id, title: :post_title },
      comments: { id: :comments_id, body: :comment_body }
    )
    
Post.joins(:comments).select(:id, :title, comments: [:body] )
0 Shares:
14 comments
  1. Hi there, i read yolur blolg from ime tto time aand i ownn a simmilar onne and i waas jyst cudious iif yoou
    gget a lot off spam comments? If soo hhow ddo youu reducxe it,
    any pougin oor anytthing yoou cann suggest? I gett
    sso mch latly it’s riving me insan so aany supportt iss vsry
    muc appreciated.

    my weeb page – redwap.pro

  2. Beef round bottom roast crock potFalse sex crime accusation collegesJada fire cumshot videosGreat free porn videosJaime elle
    nudeGay older pick spot upDownload hotel erotica 2002 xnxx Sex vixenBulgarian sexy singerBoy drinking milk titsEichin voyeur videoRachell roxxx
    gang bangAnimated sex free gameAsian cookerCum duff hilaryOlder black
    man sex stories https://xnxxfree.win Free big tits hairy pussysChubby
    anal blonde gang bangThumb pincushion tutorialOpus
    comic stripSexual harassment cases in the workplaceMature
    naked housewives videosQuality mature xnxx Nude action pictures of boysHangary pornTwo fourteen year old girls having sexCam free hot sexJeslina hashim bikiniTrnder pussyThomas vlaminck sex offenderAsian riding sausageBlowjob milking prostate vidsPeople making nice
    sexual love pornHeavy breast fuckingGay sex party videoNude barber
    las vegasDissabled gay veterans rightsMeg whites titsVintage dodge
    bus for sale

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
Read More

Nâng bao nhiêu pool connect database là phù hợp

Trong bài viết này chúng ta sẽ tìm hiểu về sự quan trọng của việc tối ưu hóa kết nối database và tìm cách đảm bảo rằng bạn đang sử dụng số lượng pool kết nối đúng đắn, tiết kiệm tài nguyên và đảm bảo hiệu suất hệ thống.
Read More

Sentry for Rails

Table of Contents Hide Đặt vấn đềGiới thiệu SentryConfig Sentry cho Rails appSummary Đặt vấn đề Trong quá…
Read More

Building a GraphQL API in Rails

Table of Contents Hide GraphQL là gì?Thiết Lập Dự Án RailsThêm DependenciesTạo Schema GraphQLĐịnh Nghĩa Các LoạiKiểm Thử…
Read More

What’s new in Ruby 3.3.0

Table of Contents Hide YJITRJITIRBRangeRails Range#overlaps?(range)Ruby < 3.3Ruby 3.3Tham khảo Là một developer có niềm đam mê sâu…