Has_many, through association

前端 未结 3 402
天涯浪人
天涯浪人 2020-12-17 07:14

Warning:Total Rails Newb (TRN). This should be a pretty basic question so I\'m hoping someone can spare a couple mins to help shed some light.

Let\'s say I have the

3条回答
  •  感动是毒
    2020-12-17 08:12

    It will be useful: http://railscasts.com/episodes/47-two-many-to-many

    class User < ActiveRecord::Base
      has_many :members
      has_many :groups, :through => :members
      has_many :groups_as_owner, :class_name => "Group"
    end
    
    class Groups < ActiveRecord::Base
      has_many :members
      has_many :users, :through => :members
      belongs_to :owner, :class_name => "User", :foreign_key => :user_id
    end
    
    class Member < ActiveRecord::Base
      belongs_to :group
      belongs_to :user
    end
    

提交回复
热议问题