How can I do a LEFT OUTER JOIN using Rails ActiveRecord?

前端 未结 8 1066
终归单人心
终归单人心 2021-02-05 06:42

I don\'t have any ideas. Could you give me any clues (like reference sites). Any help will be appreciated.

Model1: GROUP         


        
8条回答
  •  醉梦人生
    2021-02-05 07:26

    Use has_and_belongs_to_many if you just need to link users to groups. Use has_many :through if you need to store additional membership information.

    Example:

    class User < ActiveRecord::Base
      has_and_belongs_to_many :groups
    end
    
    class Group < ActiveRecord::Base
      has_and_belongs_to_many :users      
    end
    

提交回复
热议问题