Ruby code to add a user to a group

I’m looking for some example code to add a user to a group in ruby.

Here is one example:

https://github.com/discourse/discourse-patreon/blob/master/lib/patron.rb#L46

6 Likes

Thanks!

Here’s the code I ended up with

	candidate['groups'].each do |group|
		if group.user_ids.include?(user.id) then
		  # already a member
		else
		  group.add user
		  if user.primary_group == nil then
		    user.primary_group = group
		    user.save
		  end
		end
	end
2 Likes