How can I do this using Ruby on Rails?

Sorry for the vague title, but it's hard to do in one line.

I have a database full of contact information and I want to be able to put these different contacts into groups that will also be stored in the database.

So, maybe I have two groups, "colleagues" and "neighbors", I want to see a list of all my contacts and be able to add individual contacts to one or more groups.

I'm a little confused when I start with this, can I get some basic schematics of how best to implement this? Thank.

+2


source to share


3 answers


Okay, you have two models: contact and group. These two guys will obviously have their own tables (perhaps "contacts" and "groups" if you follow the Rails conventions). Since a contact can be in many groups, and a group can have many contacts, you have what is called a many-to-many relationship between these models. There are two ways you can implement in Rails: has_and_belongs_to_many

or has_many :through

. Which one is best for you will vary slightly depending on your situation.

has_and_belongs_to_many

is probably the path of least resistance. You put a couple of lines in your models like this:

# contact.rb
has_and_belongs_to_many :groups

# group.rb
has_and_belongs_to_many :contacts

      

... and create a table called "contacts_groups" with columns contact_id

and group_id

and you are mostly good. Easy peasy.



On the other hand, there are several advantages to using the association model with has_many :through

. In this approach, you create another model like GroupMembership and set up your models like this:

# contact.rb
has_many :group_memberships   # this isn't strictly required, but I'd recommend it
has_many :groups, :through => :group_memberships

# group_membership.rb
has_many :groups
has_many :contacts

# group.rb
has_many :group_memberships   # again, recommended but not required
has_many :contacts, :through => :group_memberships

      

This gives you most of the same convenience methods has_and_belongs_to_many

as and also allows you to store any additional data that might arise from the association, such as the date they joined the group, or the reason they were added. Even if you don't have any of them now, it's nice to consider adding them later. It also allows you to take a more RESTful approach to adding and removing contacts to and from groups if that interests you, as you can model it in terms of creating and destroying GroupMembership resources.

In general, I tend to lean towards the latter approach, especially since I got more options for RESTful architectures. On the other hand, if you are not worried about REST and are sure you never want to store additional membership information has_and_belongs_to_many

, it is probably easier and requires less code to work with. For more details on the differences and implementation details, see the ActiveRecord APIs .

+2


source


You can create a Ruby on Rails database:

Contact ( first_name:string, last_name:string, title:enum number:string, cell:string, notes:text, email:string ) => many_many :groups (or has_many :groups, :through=> :contact_group)
Contact_Group { group_id:integer, contact_id:integer }
Group   ( name:string ) => many_many :contacts ) (or has_many :contacts, :through=> :contact_group)

      



This might be a general idea. You can also create relational fields.

0


source


You don't want to use the has_and_belongs_to_many method. It is deprecated. Please read the API and make sure you are implementing the / has _many: union model through the approach. The API will tell you how to do this and also point out why has_and_belongs_to_many is bad.

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

0


source







All Articles