{"id":1437,"date":"2006-07-26T23:46:15","date_gmt":"2006-07-26T15:46:15","guid":{"rendered":"http:\/\/ihower.idv.tw\/blog\/archives\/1437"},"modified":"2011-04-09T02:14:07","modified_gmt":"2011-04-08T18:14:07","slug":"rails-activerecord-many-to-many","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/1437-rails-activerecord-many-to-many","title":{"rendered":"Rails \u7684 Many-to-Many \u8a0e\u8ad6"},"content":{"rendered":"<p>Rails \u7684 <a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveRecord\/Associations\/ClassMethods.html\">ActiveRecord:Associations<\/a> \u8b93\u4f60\u5efa\u7acb\u8cc7\u6599\u8868\u4e4b\u9593\u7684\u95dc\u4fc2\uff0c\u5176\u4e2d\u4e00\u5c0d\u4e00\u8ddf\u4e00\u5c0d\u591a\u95dc\u4fc2\u90fd\u9084\u7b97\u7c21\u55ae\uff0c\u591a\u5c0d\u591a\u6bd4\u8f03\u8907\u96dc\u4e00\u9ede\u3002Rails \u6709\u5169\u500b\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\uff0c<font face=\"Courier New\"><a href=\"http:\/\/wiki.rubyonrails.com\/rails\/pages\/has_and_belongs_to_many\">has_and_belongs_to_many<\/a><\/font> \u8ddf <a href=\"http:\/\/wiki.rubyonrails.com\/rails\/pages\/ThroughAssociations\">has_many :through<\/a>\u3002\u5f8c\u8005\u662f Rails 1.1 \u624d\u6709\u7684\uff0c\u6240\u4ee5 Agile Web Development with Rails \u4e00\u66f8\u4e26\u6c92\u6709\u63d0\u5230\u3002<\/p>\n<p>\u6211\u7684\u982d\u4e00\u500b\u554f\u984c\u662f\uff0c\u9019\u5169\u500b\u6709\u4ec0\u9ebc\u5dee\u5225\uff0c\u61c9\u8a72\u7528\u54ea\u4e00\u500b?? \u627e\u4e86\u4e00\u4e0b\u6709\u767c\u73fe\u6709 <a href=\"http:\/\/weblog.rubyonrails.com\/2006\/04\/21\/habtm-vs-has_many-through\/\">habtm vs has_many :through<\/a>\u00a0\u9019\u7bc7\u8ddf<strong> <\/strong><a href=\"http:\/\/blog.hasmanythrough.com\/2006\/4\/20\/many-to-many-dance-off\"><strong>Many-to-many Dance-off!<\/strong><\/a>\u3002Dance-Off \u90a3\u7bc7\u5f88\u4ed4\u7d30\u7684\u6bd4\u8f03\u5169\u8005\u7684\u5dee\u7570\uff0c\u4e5f\u7d66\u4e86\u4e00\u500b\u7bc4\u4f8b\uff0c\u503c\u5f97\u4e00\u770b\u3002<\/p>\n<p>\u4e3b\u8981\u4f86\u8aaa <font face=\"Courier New\">has_and_belongs_to_many<\/font> \u6bd4\u8f03\u7c21\u55ae\uff0c\u4f46\u662f\u90a3\u500b join table \u4e0d\u9069\u5408\u518d\u52a0\u6b04\u4f4d\u503c\uff0c\u6bd4\u5982\u8aaa\u8a72\u95dc\u806f\u5efa\u7acb\u7684\u6642\u9593\u4e4b\u985e\u7684\uff0c\u96d6\u7136 <font face=\"Courier New\">push_with_attributes<\/font> \u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u585e\u8cc7\u6599\u9032\u53bb\uff0c\u4f46\u662f\u537b\u662f\u5f88\u4e82\u7684\u7cdf\u65b9\u5f0f\uff0c\u5df2\u7d93\u4e0d\u88ab\u63a8\u85a6\u4f7f\u7528\u3002<\/p>\n<p>\u800c has_many :through \u529f\u592b\u591a\u4e00\u4e9b\uff0c\u7d50\u69cb\u66f4\u5b8c\u6574\uff0c\u7528\u65b0\u589e\u4e00\u500b join model \u7684\u65b9\u5f0f\u4f86\u5efa\u7acb\u95dc\u806f\u8cc7\u6599\u3002\u81ea\u5df1\u6709\u4e00\u500b model class\uff0c\u56e0\u6b64\u4f7f\u7528\u4e0a\u4e5f\u6bd4\u8f03\u8c50\u5bcc\u3002\u5c31\u50cf\u4e00\u822c model \u4e00\u6a23\uff0c\u5148 new \u4e00\u500b\u51fa\u4f86\uff0cassign \u597d\u90a3\u5169\u500b\u95dc\u806f\u7269\u4ef6\uff0c\u518d\u586b\u597d\u5176\u5b83\u8cc7\u6599\uff0c\u6700\u5f8c save \u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rails \u7684 ActiveRecord:Associations \u8b93\u4f60\u5efa\u7acb\u8cc7\u6599\u8868\u4e4b\u9593\u7684\u95dc\u4fc2\uff0c\u5176\u4e2d\u4e00\u5c0d\u4e00\u8ddf\u4e00\u5c0d &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/1437-rails-activerecord-many-to-many\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Rails \u7684 Many-to-Many \u8a0e\u8ad6\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,53],"tags":[],"class_list":["post-1437","post","type-post","status-publish","format-standard","hentry","category-programming","category-rails","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q6tG-nb","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/comments?post=1437"}],"version-history":[{"count":2,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1437\/revisions"}],"predecessor-version":[{"id":5581,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1437\/revisions\/5581"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=1437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=1437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=1437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}