# Urgent ActiveRecord has\_many Problem - Please Help

**URL:** <https://www.ruby-forum.com/t/urgent-activerecord-has-many-problem-please-help/135409>\
**Category:** Rails\
**Created:** [April 21, 2008, 11:16pm UTC](https://www.ruby-forum.com/t/urgent-activerecord-has-many-problem-please-help/135409 "2008-04-21T23:16:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasongarber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@jasongarber](https://www.ruby-forum.com/u/jasongarber)\
**Post date:** [April 21, 2008, 11:16pm UTC](https://www.ruby-forum.com/t/urgent-activerecord-has-many-problem-please-help/135409/1 "2008-04-21T23:16:27Z")

</div>

Hi Guys,

I’ve been looking everywhere for an answer to this but so far without  
success.

I have three models:

1. employees  
has\_many :memberships  
has\_many :projects, :through =\> :membership

2. projects  
has\_many :memberships  
has\_many :employees, :through =\> :membership

3. membership  
belongs\_to :employees  
belongs\_to :projects

Membership is used to join employees to projects.

In my projects\_controller I have a method called

def disassociate\_employee(employee\_id)  
end

In this function I want to somehow delete the database row in  
membership that joins employees t o projects.

I have no idea how to do this and has so far spend two full days  
trying to figure this out, but starting to go a bit crazy trying to  
figure this one out. 🙂

Thanks for your help!!

---

<div class="post-metadata">

**Author:** ![jasongarber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@jasongarber](https://www.ruby-forum.com/u/jasongarber)\
**Post date:** [April 21, 2008, 11:25pm UTC](https://www.ruby-forum.com/t/urgent-activerecord-has-many-problem-please-help/135409/2 "2008-04-21T23:25:50Z")

</div>

On 4/21/08, codex1 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. membership  
> In this function I want to somehow delete the database row in  
> membership that joins employees t o projects.
> 
> I have no idea how to do this and has so far spend two full days  
> trying to figure this out, but starting to go a bit crazy trying to  
> figure this one out. 🙂

Membership.find\_by\_employee\_id\_and\_project\_id( employee\_id, project\_id  
).destroy rescue nil

Or rewrite the find to use :conditions if you don’t trust the source  
of your employee and project ids.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)
