{"id":940,"date":"2025-12-25T15:33:13","date_gmt":"2025-12-25T07:33:13","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=940"},"modified":"2025-12-25T15:33:13","modified_gmt":"2025-12-25T07:33:13","slug":"%e3%80%90ruby%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c%e7%bb%88%e6%9e%81%e6%8c%87%e5%8d%97%e3%80%91%ef%bc%9a10%e4%b8%aa%e4%bd%a0%e5%bf%85%e9%a1%bb%e6%8e%8c%e6%8f%a1","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/25\/%e3%80%90ruby%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c%e7%bb%88%e6%9e%81%e6%8c%87%e5%8d%97%e3%80%91%ef%bc%9a10%e4%b8%aa%e4%bd%a0%e5%bf%85%e9%a1%bb%e6%8e%8c%e6%8f%a1\/","title":{"rendered":"\u3010Ruby\u6570\u636e\u5e93\u64cd\u4f5c\u7ec8\u6781\u6307\u5357\u3011\uff1a10\u4e2a\u4f60\u5fc5\u987b\u638c\u63e1"},"content":{"rendered":"\n<p>Ruby \u4ee5\u5176<strong>\u4f18\u96c5\u8bed\u6cd5 + \u9ad8\u5f00\u53d1\u6548\u7387<\/strong>\u6df1\u53d7\u5f00\u53d1\u8005\u559c\u7231\uff0c\u800c\u6570\u636e\u5e93\u64cd\u4f5c\u51e0\u4e4e\u8d2f\u7a7f\u6240\u6709 Ruby \u9879\u76ee\u3002<br>\u65e0\u8bba\u4f60\u4f7f\u7528\u7684\u662f <strong>Ruby on Rails<\/strong>\uff0c\u8fd8\u662f <strong>\u7eaf Ruby + ORM \/ \u539f\u751f SQL<\/strong>\uff0c\u4e0b\u9762\u8fd9 <strong>10 \u4e2a\u6570\u636e\u5e93\u64cd\u4f5c\u8981\u70b9<\/strong>\uff0c\u90fd\u662f\u4f60\u5fc5\u987b\u638c\u63e1\u7684\u786c\u6838\u57fa\u7840\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1\ufe0f\u20e3 Ruby \u4e2d\u6700\u5e38\u89c1\u7684\u6570\u636e\u5e93\u8bbf\u95ee\u65b9\u5f0f<\/h2>\n\n\n\n<p>Ruby \u64cd\u4f5c\u6570\u636e\u5e93\uff0c\u4e3b\u8981\u6709\u4e09\u79cd\u6a21\u5f0f\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u5f0f<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>ActiveRecord<\/td><td>Rails \u9ed8\u8ba4 ORM\uff08\u6700\u5e38\u7528\uff09<\/td><\/tr><tr><td>Sequel<\/td><td>\u8f7b\u91cf\u3001\u9ad8\u6027\u80fd ORM<\/td><\/tr><tr><td>\u539f\u751f SQL<\/td><td>\u4f7f\u7528\u6570\u636e\u5e93\u9a71\u52a8\u76f4\u63a5\u6267\u884c<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udc49 <strong>\u7ed3\u8bba<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Web \u9879\u76ee\uff1aActiveRecord<\/li>\n\n\n\n<li>\u9ad8\u6027\u80fd \/ \u5fae\u670d\u52a1\uff1aSequel<\/li>\n\n\n\n<li>\u6781\u81f4\u63a7\u5236\uff1a\u539f\u751f SQL<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2\ufe0f\u20e3 \u4f7f\u7528 ActiveRecord \u8fde\u63a5\u6570\u636e\u5e93<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u57fa\u672c\u914d\u7f6e\uff08Rails\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndevelopment:\n  adapter: mysql2\n  encoding: utf8mb4\n  database: demo_db\n  username: root\n  password: 123456\n  host: localhost\n\n<\/pre><\/div>\n\n\n<p>ActiveRecord \u4f1a\u81ea\u52a8\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5efa\u7acb\u8fde\u63a5\u6c60<\/li>\n\n\n\n<li>\u7ba1\u7406\u4e8b\u52a1<\/li>\n\n\n\n<li>\u5904\u7406\u7f16\u7801<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3\ufe0f\u20e3 CRUD \u64cd\u4f5c\uff08\u589e\u5220\u6539\u67e5\uff09\u5fc5\u987b\u70c2\u719f\u4e8e\u5fc3<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b0\u589e\uff08Create\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nUser.create(name: &quot;Tom&quot;, age: 20)\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u67e5\u8be2\uff08Read\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nUser.where(age: 20)\nUser.find_by(name: &quot;Tom&quot;)\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u66f4\u65b0\uff08Update\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nuser.update(age: 21)\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u5220\u9664\uff08Delete\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nuser.destroy\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udccc <strong>\u8fd9\u662f\u9762\u8bd5\u5fc5\u8003\u5185\u5bb9<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4\ufe0f\u20e3 \u67e5\u8be2\u6761\u4ef6\u4e0e\u94fe\u5f0f\u8c03\u7528\u6280\u5de7<\/h2>\n\n\n\n<p>ActiveRecord \u7684\u5f3a\u5927\u4e4b\u5904\u5728\u4e8e <strong>\u94fe\u5f0f\u67e5\u8be2<\/strong>\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nUser.where(status: 1)\n    .where(&quot;age &gt; ?&quot;, 18)\n    .order(created_at: :desc)\n    .limit(10)\n\n<\/pre><\/div>\n\n\n<p>\u4f18\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u8bfb\u6027\u9ad8<\/li>\n\n\n\n<li>\u81ea\u52a8\u9632 SQL \u6ce8\u5165<\/li>\n\n\n\n<li>\u5ef6\u8fdf\u6267\u884c\uff08Lazy Load\uff09<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5\ufe0f\u20e3 \u5173\u8054\u5173\u7cfb\uff08Association\uff09\u662f\u6838\u5fc3\u80fd\u529b<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nclass Order &amp;lt; ApplicationRecord\n  belongs_to :user\nend\n\nclass User &amp;lt; ApplicationRecord\n  has_many :orders\nend\n\n<\/pre><\/div>\n\n\n<p>\u4f7f\u7528\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nuser.orders\norder.user\n\n<\/pre><\/div>\n\n\n<p>\u26a0\ufe0f \u4e0d\u7406\u89e3\u5173\u8054\uff0c\u7b49\u4e8e <strong>Rails \u53ea\u4f1a\u5199\u4e00\u534a<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6\ufe0f\u20e3 N+1 \u67e5\u8be2\u95ee\u9898\u4e0e\u6027\u80fd\u4f18\u5316<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u9519\u8bef\u793a\u4f8b\uff08N+1\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nUser.all.each do |u|\n  puts u.orders.count\nend\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u6b63\u786e\u65b9\u5f0f\uff08includes\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nUser.includes(:orders).each do |u|\n  puts u.orders.size\nend\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udccc <strong>N+1 \u662f Rails \u6027\u80fd\u6740\u624b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7\ufe0f\u20e3 \u4e8b\u52a1\uff08Transaction\uff09\u5fc5\u987b\u4f1a\u7528<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nActiveRecord::Base.transaction do\n  user.save!\n  order.save!\nend\n\n<\/pre><\/div>\n\n\n<p>\u4f5c\u7528\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4fdd\u8bc1\u6570\u636e\u4e00\u81f4\u6027<\/li>\n\n\n\n<li>\u51fa\u9519\u81ea\u52a8\u56de\u6eda<\/li>\n\n\n\n<li>\u91d1\u878d \/ \u8ba2\u5355\u7cfb\u7edf\u5fc5\u5907<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8\ufe0f\u20e3 \u4f7f\u7528\u539f\u751f SQL\uff08\u5fc5\u8981\u65f6\uff09<\/h2>\n\n\n\n<p>\u6709\u4e9b\u590d\u6742\u67e5\u8be2 ORM \u5199\u8d77\u6765\u5f88\u96be\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsql = &quot;SELECT * FROM users WHERE age &gt; ?&quot;\nUser.find_by_sql(&#x5B;sql, 18])\n\n<\/pre><\/div>\n\n\n<p>\u2705 \u4ec0\u4e48\u65f6\u5019\u7528 SQL\uff1f<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u62a5\u8868<\/li>\n\n\n\n<li>\u590d\u6742 JOIN<\/li>\n\n\n\n<li>\u6027\u80fd\u74f6\u9888\u573a\u666f<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">9\ufe0f\u20e3 \u6570\u636e\u5e93\u7d22\u5f15\u4e0e\u8fc1\u79fb\uff08Migration\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u521b\u5efa\u7d22\u5f15<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nadd_index :users, :email, unique: true\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u4fee\u6539\u8868\u7ed3\u6784<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nrails generate migration AddAgeToUsers age:integer\nrails db:migrate\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udccc <strong>\u4e0d\u4f1a Migration = \u4e0d\u4f1a Rails<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd1f \u9632\u6b62 SQL \u6ce8\u5165\u4e0e\u5b89\u5168\u6700\u4f73\u5b9e\u8df5<\/h2>\n\n\n\n<p>\u274c \u9519\u8bef\u5199\u6cd5\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nUser.where(&quot;name = &#039;#{params&#x5B;:name]}&#039;&quot;)\n\n<\/pre><\/div>\n\n\n<p>\u2705 \u6b63\u786e\u5199\u6cd5\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nUser.where(&quot;name = ?&quot;, params&#x5B;:name])\n\n<\/pre><\/div>\n\n\n<p>ActiveRecord \u9ed8\u8ba4\u63d0\u4f9b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53c2\u6570\u7ed1\u5b9a<\/li>\n\n\n\n<li>\u8f6c\u4e49\u673a\u5236<\/li>\n\n\n\n<li>\u5b89\u5168\u67e5\u8be2\u63a5\u53e3<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 \u8fdb\u9636\u8865\u5145\uff08\u9ad8\u624b\u5fc5\u770b\uff09<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>pluck<\/code> \u51cf\u5c11\u5185\u5b58\u5360\u7528<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>select<\/code> \u9650\u5236\u5b57\u6bb5<\/li>\n\n\n\n<li>\u5584\u7528 <code>explain<\/code> \u5206\u6790 SQL<\/li>\n\n\n\n<li>\u5408\u7406\u8bbe\u7f6e\u8fde\u63a5\u6c60\u5927\u5c0f<\/li>\n\n\n\n<li>\u907f\u514d\u5728\u5faa\u73af\u4e2d\u9891\u7e41\u67e5\u8be2\u6570\u636e\u5e93<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u603b\u7ed3\u901f\u89c8\u8868<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u80fd\u529b<\/th><th>\u662f\u5426\u5fc5\u987b<\/th><\/tr><\/thead><tbody><tr><td>CRUD<\/td><td>\u2705<\/td><\/tr><tr><td>\u5173\u8054\u67e5\u8be2<\/td><td>\u2705<\/td><\/tr><tr><td>\u4e8b\u52a1<\/td><td>\u2705<\/td><\/tr><tr><td>\u6027\u80fd\u4f18\u5316<\/td><td>\u2705<\/td><\/tr><tr><td>\u539f\u751f SQL<\/td><td>\u2b50\u2b50<\/td><\/tr><tr><td>\u6570\u636e\u5e93\u7d22\u5f15<\/td><td>\u2b50\u2b50\u2b50<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Ruby \u4ee5\u5176\u4f18\u96c5\u8bed\u6cd5 + \u9ad8\u5f00\u53d1\u6548\u7387\u6df1\u53d7\u5f00\u53d1\u8005\u559c\u7231\uff0c\u800c\u6570\u636e\u5e93\u64cd\u4f5c\u51e0\u4e4e\u8d2f\u7a7f\u6240\u6709 &#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/25\/%e3%80%90ruby%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c%e7%bb%88%e6%9e%81%e6%8c%87%e5%8d%97%e3%80%91%ef%bc%9a10%e4%b8%aa%e4%bd%a0%e5%bf%85%e9%a1%bb%e6%8e%8c%e6%8f%a1\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-940","post","type-post","status-publish","format-standard","hentry","category-12"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=940"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/940\/revisions"}],"predecessor-version":[{"id":941,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/940\/revisions\/941"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}