{"id":223,"date":"2025-11-26T10:08:07","date_gmt":"2025-11-26T02:08:07","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=223"},"modified":"2025-11-26T10:08:07","modified_gmt":"2025-11-26T02:08:07","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%e7%9a%84activerecord%e5%ae%9e","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/11\/26\/%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%e7%9a%84activerecord%e5%ae%9e\/","title":{"rendered":"\u3010Ruby\u6570\u636e\u5e93\u64cd\u4f5c\u7ec8\u6781\u6307\u5357\u3011\uff1a10\u4e2a\u4f60\u5fc5\u987b\u638c\u63e1\u7684ActiveRecord\u5b9e\u6218\u6280\u5de7"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u7bc7 <strong>\u300aRuby \u6570\u636e\u5e93\u64cd\u4f5c\u7ec8\u6781\u6307\u5357\uff1a10 \u4e2a\u4f60\u5fc5\u987b\u638c\u63e1\u7684 ActiveRecord \u5b9e\u6218\u6280\u5de7\u300b<\/strong>\u2014\u2014\u9002\u5408\u505a\u6587\u7ae0\u3001\u6559\u7a0b\u6216\u89c6\u9891\u811a\u672c\u3002\u5185\u5bb9\u7ed3\u6784\u6e05\u6670\u3001\u793a\u4f8b\u5b8c\u6574\u3001\u53ef\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Ruby \u6570\u636e\u5e93\u64cd\u4f5c\u7ec8\u6781\u6307\u5357\uff1a10 \u4e2a\u4f60\u5fc5\u987b\u638c\u63e1\u7684 ActiveRecord \u5b9e\u6218\u6280\u5de7<\/strong><\/h1>\n\n\n\n<p>ActiveRecord \u662f Ruby on Rails \u7684\u7075\u9b42\u4e4b\u4e00\u3002<br>\u53ea\u8981\u638c\u63e1\u4e86 ActiveRecord\uff0c\u4f60\u5c31\u80fd\u8f7b\u677e\u5b8c\u6210 <strong>\u6570\u636e\u5e93\u5efa\u6a21\u3001\u67e5\u8be2\u3001\u5173\u8054\u3001\u4e8b\u52a1\u3001\u6027\u80fd\u4f18\u5316<\/strong> \u7b49\u6838\u5fc3\u4efb\u52a1\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u8fd9 10 \u4e2a\u6280\u5de7\uff0c\u662f\u6bcf\u4e2a Ruby \u5f00\u53d1\u8005\u90fd\u5fc5\u987b\u638c\u63e1\u7684\u201c\u786c\u6838\u6280\u80fd\u201d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u4f7f\u7528 Migrations \u7ba1\u7406\u6570\u636e\u5e93\u7ed3\u6784<\/strong><\/h2>\n\n\n\n<p>Migration \u662f Rails \u4e2d\u7684\u6570\u636e\u5e93\u7248\u672c\u63a7\u5236\u5de5\u5177\uff0c\u8ba9\u4f60\u7684\u6570\u636e\u5e93\u7ed3\u6784 <strong>\u53ef\u8ffd\u8e2a\u3001\u53ef\u56de\u6eda\u3001\u53ef\u534f\u4f5c<\/strong>\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nclass CreateUsers &amp;lt; ActiveRecord::Migration&#x5B;7.1]\n  def change\n    create_table :users do |t|\n      t.string :name\n      t.string :email, index: true\n      t.timestamps\n    end\n  end\nend\n\n<\/pre><\/div>\n\n\n<p>\u5e38\u7528\u547d\u4ee4\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nrails db:migrate\nrails db:rollback\nrails db:migrate:status\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u5229\u7528 Model Validations \u4fdd\u8bc1\u6570\u636e\u6b63\u786e\u6027<\/strong><\/h2>\n\n\n\n<p>ActiveRecord \u7684\u9a8c\u8bc1\u5668\u8ba9\u4f60\u8f7b\u677e\u5b9e\u73b0\u6570\u636e\u201c\u5b88\u95e8\u201d\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nclass User &amp;lt; ApplicationRecord\n  validates :email, presence: true\n  validates :age, numericality: { greater_than_or_equal_to: 18 }\nend\n\n<\/pre><\/div>\n\n\n<p>\u5e38\u7528\u9a8c\u8bc1\u5668\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>presence<\/code><\/li>\n\n\n\n<li><code>uniqueness<\/code><\/li>\n\n\n\n<li><code>length<\/code><\/li>\n\n\n\n<li><code>numericality<\/code><\/li>\n\n\n\n<li><code>format<\/code><\/li>\n\n\n\n<li><code>inclusion \/ exclusion<\/code><\/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\"><strong>3. \u5584\u7528 Associations\uff08\u5173\u8054\uff09\u8ba9\u8868\u4e4b\u95f4\u81ea\u7136\u5bf9\u8bdd<\/strong><\/h2>\n\n\n\n<p>\u6700\u5e38\u89c1\u7684\u56db\u5927\u5173\u8054\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nclass User &amp;lt; ApplicationRecord\n  has_many :posts\nend\n\nclass Post &amp;lt; ApplicationRecord\n  belongs_to :user\n  has_one :category\n  has_many :comments\nend\n\n<\/pre><\/div>\n\n\n<p>\u5c0f\u6280\u5de7\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>dependent: :destroy<\/code> \u81ea\u52a8\u5220\u9664\u5173\u8054\u6570\u636e<\/li>\n\n\n\n<li><code>inverse_of<\/code> \u663e\u8457\u51cf\u5c11\u67e5\u8be2\u6b21\u6570<\/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\"><strong>4. Eager Loading \u89e3\u51b3 N+1 \u6027\u80fd\u9677\u9631<\/strong><\/h2>\n\n\n\n<p>\u907f\u514d\u6bcf\u6761\u8bb0\u5f55\u89e6\u53d1\u4e00\u6b21\u67e5\u8be2\uff1a<\/p>\n\n\n\n<p>\u274c \u4f4e\u6548\u505a\u6cd5\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nUser.all.each { |u| puts u.posts.count }\n\n<\/pre><\/div>\n\n\n<p>\u2714 \u9ad8\u6548\u505a\u6cd5\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nUser.includes(:posts).each { |u| puts u.posts.count }\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u4f18\u96c5\u4f7f\u7528 Query Methods \u6784\u5efa\u590d\u6742\u67e5\u8be2<\/strong><\/h2>\n\n\n\n<p>ActiveRecord \u7684\u67e5\u8be2\u65b9\u6cd5\u94fe\u662f\u5b83\u7684\u201c\u827a\u672f\u6027\u201d\u6240\u5728\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nUser.where(active: true)\n    .where(&quot;created_at &gt; ?&quot;, 1.week.ago)\n    .order(created_at: :desc)\n    .limit(20)\n\n<\/pre><\/div>\n\n\n<p>\u5e38\u89c1 Query Methods\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>where<\/code><\/li>\n\n\n\n<li><code>order<\/code><\/li>\n\n\n\n<li><code>group<\/code><\/li>\n\n\n\n<li><code>pluck<\/code><\/li>\n\n\n\n<li><code>select<\/code><\/li>\n\n\n\n<li><code>distinct<\/code><\/li>\n\n\n\n<li><code>limit<\/code><\/li>\n\n\n\n<li><code>joins<\/code> \/ <code>left_joins<\/code><\/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\"><strong>6. \u4f7f\u7528 Scopes \u91cd\u590d\u903b\u8f91\u6a21\u5757\u5316<\/strong><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nclass User &amp;lt; ApplicationRecord\n  scope :active, -&gt; { where(active: true) }\n  scope :recent, -&gt; { order(created_at: :desc) }\nend\n\nUser.active.recent\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u5229\u7528 Transactions \u4fdd\u8bc1\u6570\u636e\u4e00\u81f4\u6027<\/strong><\/h2>\n\n\n\n<p>Bank-transfer \u573a\u666f\u6c38\u8fdc\u7684\u7ecf\u5178\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nActiveRecord::Base.transaction do\n  sender.update!(balance: sender.balance - 100)\n  receiver.update!(balance: receiver.balance + 100)\nend\n\n<\/pre><\/div>\n\n\n<p>\u5173\u952e\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>transaction<\/code> + <code>raise<\/code> \/ <code>update!<\/code><\/li>\n\n\n\n<li>\u81ea\u52a8\u56de\u6eda<\/li>\n\n\n\n<li>\u591a\u8868\u591a\u6b65\u9aa4\u539f\u5b50\u64cd\u4f5c<\/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\"><strong>8. \u4f7f\u7528 Callbacks \u7b80\u5316\u4e1a\u52a1\u6d41\u7a0b\uff08\u4f46\u6ce8\u610f\u9002\u5ea6\uff09<\/strong><\/h2>\n\n\n\n<p>\u5e38\u89c1\u751f\u547d\u5468\u671f\u94a9\u5b50\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>before_create<\/code><\/li>\n\n\n\n<li><code>after_create<\/code><\/li>\n\n\n\n<li><code>before_save<\/code><\/li>\n\n\n\n<li><code>after_commit<\/code><\/li>\n<\/ul>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nclass Order &amp;lt; ApplicationRecord\n  after_create :send_notification\n\n  def send_notification\n    NotificationService.send_order_created(self)\n  end\nend\n\n<\/pre><\/div>\n\n\n<p>\u26a0 \u4e0d\u8981\u6ee5\u7528 callbacks \u505a\u590d\u6742\u4e1a\u52a1\uff0c\u5426\u5219\u96be\u8c03\u8bd5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>9. \u4f7f\u7528 Arel \u63d0\u5347\u67e5\u8be2\u7075\u6d3b\u5ea6<\/strong><\/h2>\n\n\n\n<p>\u5f53 ActiveRecord \u7684\u94fe\u5f0f\u67e5\u8be2\u4e0d\u591f\u65f6\uff0c\u5c31\u7528 Arel\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nusers = User.arel_table\nUser.where(users&#x5B;:age].gt(18).and(users&#x5B;:status].eq(&quot;active&quot;)))\n\n<\/pre><\/div>\n\n\n<p>Arel \u7684\u5f3a\u9879\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u52a8\u6001\u62fc\u63a5\u6761\u4ef6<\/li>\n\n\n\n<li>\u751f\u6210\u5b89\u5168 SQL<\/li>\n\n\n\n<li>\u6784\u5efa\u590d\u6742\u903b\u8f91\u8868\u8fbe\u5f0f<\/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\"><strong>10. \u4f7f\u7528 Optimistic Locking \u907f\u514d\u5e76\u53d1\u5199\u5165\u51b2\u7a81<\/strong><\/h2>\n\n\n\n<p>\u53ea\u9700\u6dfb\u52a0 <code>lock_version<\/code> \u5b57\u6bb5\uff0cRails \u81ea\u52a8\u63a5\u7ba1\u5e76\u53d1\u51b2\u7a81\u68c0\u6d4b\u3002<\/p>\n\n\n\n<p>Migration\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nadd_column :users, :lock_version, :integer\n\n<\/pre><\/div>\n\n\n<p>\u66f4\u65b0\u65f6\u81ea\u52a8\u68c0\u67e5\u7248\u672c\u4e00\u81f4\u6027\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nuser.update(name: &quot;Alice&quot;)\n\n<\/pre><\/div>\n\n\n<p>\u5982\u679c\u5176\u4ed6\u7ebf\u7a0b\u540c\u65f6\u5199\u5165\uff0c\u4f1a\u629b\u51fa\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nActiveRecord::StaleObjectError\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>\u9644\u5f55\uff1aActiveRecord \u5fc5\u5907\u547d\u4ee4\u901f\u67e5\u8868<\/strong><\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5e38\u7528\u547d\u4ee4<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>rails db:migrate<\/code><\/td><td>\u6267\u884c\u6240\u6709\u8fc1\u79fb<\/td><\/tr><tr><td><code>rails db:rollback<\/code><\/td><td>\u56de\u6eda\u4e00\u6b65<\/td><\/tr><tr><td><code>rails db:seed<\/code><\/td><td>\u8fd0\u884c seeds \u6570\u636e<\/td><\/tr><tr><td><code>rails console<\/code><\/td><td>\u6253\u5f00 AR \u8c03\u8bd5\u73af\u5883<\/td><\/tr><tr><td><code>rails db<\/code><\/td><td>\u6253\u5f00\u6570\u636e\u5e93\u5ba2\u6237\u7aef<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h1>\n\n\n\n<p>ActiveRecord \u4e0d\u53ea\u662f\u4e00\u4e2a ORM\uff0c\u5b83\u8bbe\u8ba1\u5f97\u8db3\u591f\u4f18\u96c5\uff0c\u8ba9\u4f60\u53ef\u4ee5\u50cf\u5199\u81ea\u7136\u8bed\u8a00\u4e00\u6837\u6784\u5efa\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u638c\u63e1\u672c\u6587\u7684 10 \u4e2a\u6280\u5de7\uff0c\u4f60\u5c31\u80fd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5199\u51fa\u66f4\u5b89\u5168\u7684\u6570\u636e\u6a21\u578b<\/li>\n\n\n\n<li>\u6784\u5efa\u66f4\u9ad8\u6027\u80fd\u7684\u67e5\u8be2<\/li>\n\n\n\n<li>\u8ba9\u4e1a\u52a1\u903b\u8f91\u66f4\u7b80\u6d01<\/li>\n\n\n\n<li>\u8f7b\u677e\u5e94\u5bf9\u5b9e\u9645\u9879\u76ee\u6570\u636e\u5e93\u6311\u6218<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u7bc7 \u300aRuby \u6570\u636e\u5e93\u64cd\u4f5c\u7ec8\u6781\u6307\u5357\uff1a10 \u4e2a\u4f60\u5fc5\u987b\u638c\u63e1\u7684 Active&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/11\/26\/%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%e7%9a%84activerecord%e5%ae%9e\/\">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":[13],"tags":[],"class_list":["post-223","post","type-post","status-publish","format-standard","hentry","category-ruby"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/223","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=223"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":224,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/223\/revisions\/224"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}