has_many belongs_to

users

create_table :users do |t|
 t.timestamps
end

groups

create_table :groups do |t|

  t.integer :from_id
  t.integer :to_id
  t.boolean :is_accept, default: false

end

 

rails 3.2

User

has_many :groups, class_name: 'Group', foreign_key: :from_id, conditions: 'accept=1'
has_many :group_users, through: :groups, source: :follow_user

Group

belongs_to :user, class_name: 'User', foreign_key: :from_id
belongs_to :follow_user, class_name: 'User', foreign_key: :to_id

 

rails4

User

has_many :groups, -> { where accept: true }, class_name: 'Group', foreign_key: :from_id
has_many :group_users, through: :groups, source: :follow_user

Group

belongs_to :user, class_name: 'User', foreign_key: :from_id
belongs_to :follow_user, class_name: 'User', foreign_key: :to_id

 

posted on 2014-05-29 14:30  qinyan20  阅读(188)  评论(0编辑  收藏  举报