Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

How to assign 'users' to 'groups', Associations etc

Ok, so I've got a User model and a Group model.

User belongs_to :group

Group has_many :users, dependent: :destroy

Rather than CREATE a User from within a group, I want to create Groups (easy), and ASSIGN Users to Groups.

How do I do that?

F*ck it, maybe I'll just use this? https://github.com/dwbutler/groupify

Some of that gem's features would be useful, actually, such as the ability to assign a User as a 'manager' of a group and be able to change the 'manager' also at a later date.

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

I'm not 100% sure what your question is exactly, but:

1) Don't destroy the users along with the group! What if they want to join another group in the future when their current group is destroyed? Unless this is intended behavior.

2) What's your schema for groups and users? I want to show you a few examples of how to assign things both ways, if that is what you're asking ;)

Good point on no.1 - it was a brainfart.

create_table "groups", force: :cascade do |t|
    t.text     "description"
    t.string   "level"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end
create_table "users", force: :cascade do |t|
    t.string   "email",            null: false
    t.string   "crypted_password"
    t.string   "salt"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "name"
  end
Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

Alright, first you need to add group_id (integer, preferably with index) field to your users to be able to actually assign them to groups.

has_many and belongs_to basically give you additional methods that do database queries for you. So go to the console and try this:

So when you create a group, you can access all its assigned users like this:

Group.first.users

This will return an empty array at first if no users are assigned to that group. User.first.group should also give you some empty value (nil), since that user does has his group_id set to nil.

Now you can assign users to groups using simple Ruby syntax:

group = Group.first
user = User.first
group.users << user

And the user gets his group_id set to this first group's id and when you call Group.first.users or User.first.group, they are no longer empty or nil.

If you really want to understand activerecord associations, you should read this book: http://easyactiverecord.com/