smatter
smatter

Reputation: 29178

Rails model definition

What does the following model definition (lines 2-4) mean in rails? What is:members symbol denote here? I don't find it used elsewhere. I am new to rails and trying to work on a project.

class UserGroup < ActiveRecord::Base
has_and_belongs_to_many :members,
:join_table => 'membership',
:class_name => 'User'
end

Upvotes: 2

Views: 233

Answers (1)

spike
spike

Reputation: 10004

This has_and_belongs_to_many line sets up a HABTM relationship between the UserGroup model and the User model. The name of the association is 'members' and it is stored in the "membership" table.

This means you if you have an instance g of the UserGroup model, you can do g.members and get back all of the Users that are linked to g in the membership table.

Read through this page for more information: http://guides.rubyonrails.org/association_basics.html#has_and_belongs_to_many-association-reference

Upvotes: 6

Related Questions