刘华佼的全栈学习记录

如何建立xxx成员关系表

| Comments

目标

建立xxx成员关系表

步骤一:

要建立xxx成员数据表,首先要新建XxxRelationship的model,这个数据表有两个栏位:xxx_id和user_id
rails g model xxx_relationship xxx_id:integer user_id:integer

步骤二:

rake db:migrate

步骤三:设定使用者参与的所有群

app/models/user.rb 加入:

has_many :xxx_relationships
has_many :participated_xxxs, :through => :xxx_relationships, :source => :xxx

app/models/xxx_relationship.rb加入:

  belongs_to :xxx
  belongs_to :user

user.participated_xxxs即表示user参与的所有群

步骤四:设定xxx内的所有users

app/models/xxx.rb加入:
```
has_many :xxx_relationships
has_many :members, through: :xxx_relationships, source: :user

步骤五:rails console测试

u=User.first
g=Xxx.first
g.members << u
g.members
u.participated_xxxs

Comments

comments powered by Disqus