Skip to content

afeiship/rails-module-many2many

Repository files navigation

rails-module-many2many

Rails module for many to many model associations.

step by step

  • create models:
# 多对多 主体1
rails g model Article name:string published_on:date content:text

# 多对多 主体2
rails g model Tag name:string

# 多对多 关系体:一般命名:主体1s + 主体2 (主体1/主体2 哪个更重要,就把哪个放前面)
rails g model ArticlesTag article_id:integer tag_id:integer

# 推荐用下面的方式
# 1. 可以产生 belongs_to 关系
# 2. 可以 migration 中产生如下关系对
    # t.references :article, null: false, foreign_key: true
    # t.references :tag, null: false, foreign_key: true
# 3. 这里注意顺便 article/tag,后面的references 也对应顺序
rails g model ArticlesTag article:references tag:references

solution1

这种方式会自动处理好 destroy 的情况。 这种简洁的方式,适合于你的 关联表 ArticlesTag 这个没有什么特别的含义,可以直接使用这种。

  • create associations1
# model/article.rb
class Article < ApplicationRecord
    has_and_belongs_to_many :tags
end

# model/tag.rb
class Tag < ApplicationRecord
    has_and_belongs_to_many :articles
end

destroy

a1 = Article.first
# will show tags
a1.tags

# will show many records
ArticlesTag.all

# destroy
a1.destory

# 相关联的内容会被删除掉
a1.tags # []
ArticlesTag.all # []

solution2

  • create associations2(Maybe this is another way)
class Article < ActiveRecord::Base
    has_many :articles_tags
    has_many :tags, :through => :articles_tags
end

class ArticlesTags < ActiveRecord::Base
    belongs_to :article
    belongs_to :tag
end

class Tag < ActiveRecord::Base
    has_many :articles_tags
    has_many :contacts, :through => :articles_tags
end

rake tasks:

rake db:migrate
rake db:seed

seed code:

a1 = Article.first
a1.tags<<Tag.first
a1.tags<<Tag.second
a1.tags<<Tag.third

a1.tag_ids
# [1,2,3]

About

Rails module for many to many model associations.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published