# Appending a record in many to many relationship

**URL:** <https://www.ruby-forum.com/t/appending-a-record-in-many-to-many-relationship/127819>\
**Category:** Rails\
**Created:** [February 1, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/appending-a-record-in-many-to-many-relationship/127819 "2008-02-01T15:40:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [February 1, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/appending-a-record-in-many-to-many-relationship/127819/1 "2008-02-01T15:40:00Z")

</div>

Hello,  
I have the following mailinglist and people model

class Mailinglist \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :people  
end

class Person \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :mailinglists  
end

Now, i have created a form to search people. In the result of the search  
there is  
a checkbox beside each result. i tick the checkbox and write the  
mailinglist name in the textbox provided on the results page.

THE PROBLEM IS IF THE LIST IS ALREADY CREATED AND IF I WANT TO ADD  
ANOTHER PERSON TO THE LIST HOW CAN I DO THAT.

IF I USE UPDATE\_ATTRIBUTES THE OLD PEOPLE ARE MOVED OUT OF THE LIST AND  
ONLY THE NEW ONES are added. i want to add the new people to the list  
without removing the old ones.  
my three tables are  
people  
mailinglists  
mailinglists\_people  
i HAVE A COMBO BOX SELECTION on the result page TO SELECT THE LIST IN  
WHICH I NEED TO ADD PEOPLE.

Thank you.

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [February 1, 2008, 3:52pm UTC](https://www.ruby-forum.com/t/appending-a-record-in-many-to-many-relationship/127819/2 "2008-02-01T15:52:58Z")

</div>

this should do it:

@mailing\_list = MailingList.find(…)  
@person = Person.find(…)

@mailing\_list.people \<\< @person

only that:

class Mailinglist \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :people  
end

should be:

class Mailinglist \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :persons  
end

then my last line:  
@mailing\_list.people \<\< @person  
should be  
@mailing\_list.persons \<\< @person

or you have used :foreign\_key ?

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [February 1, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/appending-a-record-in-many-to-many-relationship/127819/3 "2008-02-01T18:02:09Z")

</div>

@Thorsten

> end  
> The association is defined correctly. Check your console:

“person”.pluralize

=\> “people”

@Ank  
You might also consider using a named association rather than  
has\_and\_belongs\_to\_many because it gives you much more control over  
the association.

For example:

class MailingList \< ARec::Base  
has\_many :subscriptions  
has\_many :people, :through =\> :subscriptions  
end

class Subscription \< ARec::Base  
belongs\_to :mailing\_list  
belongs\_to :person  
end

class People  
has\_many :subscriptions  
has\_many :mailing\_lists, :through =\> :subscriptions

# for convenience

def subscribe(mailing\_list)  
subscriptions.create(:mailing\_list=\>mailing\_list)  
end  
end

Then Thorsten’s code would be:  
@mailing\_list = MailingList.find(…)  
@person = Person.find(…)  
@person.subscribe(@mailing\_list) if @mailing\_list && @person

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [February 1, 2008, 7:01pm UTC](https://www.ruby-forum.com/t/appending-a-record-in-many-to-many-relationship/127819/4 "2008-02-01T19:01:52Z")

</div>

Thorsten M. wrote:

> this should do it:
> 
> @mailing\_list = MailingList.find(…)  
> @person = Person.find(…)
> 
> @mailing\_list.people \<\< @person
> 
> only that:
> 
> class Mailinglist \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many :people  
> end

Thanks a ton…

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [February 1, 2008, 6:09pm UTC](https://www.ruby-forum.com/t/appending-a-record-in-many-to-many-relationship/127819/5 "2008-02-01T18:09:42Z")

</div>

Hi Andy,  
thank you for correcting me there, english is not my  
native language, so i went for the simple form

and of course you’re right, i can’t even think  
of any code where i ever used the basic habtm relationship myself 🙂
