`
hideto
  • 浏览: 2666788 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

每天一剂Rails良药之Self-referential Many-to-Many Relationships

    博客分类:
  • Ruby
阅读更多
考虑一个UserStory,Person 1添加Person 2为好友,怎样让Person 2的好友列表中也有Person 1?
这里Person对自己Many-to-Many关联。

class AddPeopleAndTheirFriendsRelationship < ActiveRecord::Migration
  def self.up
    create_table :people do |t|
      t.column "name", :string
    end
    create_table :friendships do |t|
      t.column "person_id", :integer
      t.column "friend_id", :integer
    end
  end
  def self.down
    drop_table :people
    drop_table :friendships
  end
end

class Person < ActiveRecord::Base
  has_and_belongs_to_many :friends,
    :class_name => "Person",
    :join_table => "friendships",
    :association_foreign_key => "friend_id",
    :foreign_key => "person_id",
    :after_add => :be_friendly_to_friend,
    :after_remove => :no_more_mr_nice_guy
    def be_friendly_to_friend(friend)
      friend.friends << self unless friend.friends.include?(self)
    end
    def no_more_mr_nice_guy(friend)
      friend.friends.delete(self) rescue nil
    end
end

这里:after_add和:after_remove是不是有点filter的意思?
分享到:
评论

相关推荐

    rails-hackernews-reddit-producthunt-clone, 黑客 news/reddit/social 链接分享网站 用 Rails 构建.zip

    rails-hackernews-reddit-producthunt-clone, 黑客 news/reddit/social 链接分享网站 用 Rails 构建 Rails 上的 Reddit-Hackernews-ProductHunt克隆演示 这是一个 readme.md的Ruby on Rails 应用程序,模仿了 Hacker...

    Agile Web Development with Rails-Second Edition-Beta一书例子

    《Agile Web Development with Rails-Second Edition-Beta》是一本专注于使用Ruby on Rails进行敏捷Web开发的书籍。这本书的第二版beta版提供了关于如何利用Rails框架高效构建动态、响应式网站的深入指导。作者们...

    rails3-mongoid-devise, 示例 Rails 3.2应用,带有数据 Mongoid,用于验证.zip

    rails3-mongoid-devise, 示例 Rails 3.2应用,带有数据 Mongoid,用于验证 Rails 4.1有关设计的Rails 4.1示例应用程序,请参见:rails设计有一个用于设计的教程:Rails 设计教程。类似示例和教程这是来自 RailsApps...

    rails-documentation-2-0-2

    rails-documentation-2-0-2

    rails-documentation-1-2-1.zip

    标题 "rails-documentation-1-2-1.zip" 暗示这是一份关于 Ruby on Rails 框架的文档,版本为 1.2.1。Ruby 是一种面向对象的编程语言,而 Rails 是一个基于 Ruby 的开源 Web 应用程序框架,遵循 Model-View-...

    awesome-rails-gem-zh_CN, Rails 常用 Gem 列表 - Awesome Rails Gem 中文版.zip

    Rails,全称Ruby on Rails,是一款基于Ruby语言的开源Web应用程序框架,以其“Don't Repeat Yourself”(DRY)和“Convention over Configuration”(CoC)的原则,深受开发者喜爱。Awesome Rails Gem 是一个广泛...

    rails-beginner-s-guide

    rails-beginner-s-guide是Rails 指导手册,帮组学习了解rails开发

    rails-documentation-1-2-0-rc1.chm

    rails-documentation-1-2-0-rc1.chm

    rails-chm-2-0-2.rar

    `rails-documentation-2-0-2.chm` 文件详细涵盖了这些概念,包含了关于Rails 2.0.2的API参考、教程和指南。通过仔细阅读和实践,开发者能够深入理解Rails的工作原理,并有效地开发出高效、可维护的Web应用。

    rails-dev-box, 面向 Ruby on Rails 核心开发的虚拟机.zip

    rails-dev-box, 面向 Ruby on Rails 核心开发的虚拟机 用于 Ruby on Rails 核心开发的虚拟机简介注意:这个虚拟机不是为 Rails 应用程序开发而设计的,只是为。 这个项目自动设置开发环境,以便在 Ruby on Rails ...

    Rails管理框架upmin-admin.zip

    upmin-admin 是一个为 Rails 应用开发的开源管理框架。用来管理 Rails 应用中各种对象(如 Model、View 和 Controller )。 标签:upmin

    关于rails 3.1 cucumber-rails 1.2.0

    Rails 3.1 和 Cucumber-Rails 1.2.0 是两个在Web开发领域非常重要的工具,尤其对于Ruby on Rails框架的测试和自动化流程。本文将深入探讨这两个组件,以及它们如何协同工作来增强软件开发的效率和质量。 首先,...

    sclo-ror42-rubygem-rails-html-sanitizer-1.0.3-1.el7.noarch.rpm

    官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装

    rails-react-components-源码.rar

    本文将深入探讨"rails-react-components-源码.rar"中的关键知识点,帮助开发者理解如何在Rails应用中集成React组件。 1. **React组件化开发** React的核心概念是组件,它允许我们将UI拆分为独立、可重用的部分。在...

    flexigrid+rails 新手代码-。-

    Flexigrid 是一个 JavaScript 库,常用于创建可扩展、灵活的表格,而 Ruby on Rails 是一个流行的开源 Web 应用程序框架,遵循 MVC(模型-视图-控制器)架构模式。下面将详细解释这两个技术以及它们在实际应用中的...

    RVM+Ruby1.9.3+Rails3(1-Cygwin 安装配置)

    在 Windows 7 环境下搭建 Rails 3 开发环境是一项颇具挑战性的任务,尤其是当涉及到 Cygwin、Ruby、Rails 以及一系列其他必要的组件时。本文将详细阐述如何在 Windows 7 系统上利用 Cygwin 进行环境搭建,包括 Git、...

    Scrum Poker in Rails5, docker-compose

    这个配置会创建一个Rails应用服务、一个PostgreSQL数据库服务和一个Redis服务,确保所有组件正确连接并运行。 总之,结合Rails5和Docker Compose,我们可以构建一个强大的Scrum Poker应用,提供实时的估算讨论环境...

Global site tag (gtag.js) - Google Analytics