# Self-referencial habtm relationship

**URL:** <https://www.ruby-forum.com/t/self-referencial-habtm-relationship/71436>\
**Category:** Rails\
**Created:** [February 21, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/self-referencial-habtm-relationship/71436 "2006-02-21T18:17:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![randy\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f9ae1b/32.png) [@randy\_s](https://www.ruby-forum.com/u/randy_s)\
**Post date:** [February 21, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/self-referencial-habtm-relationship/71436/1 "2006-02-21T18:17:33Z")

</div>

Heyo!

I am setting up a self-referencial habtm relationship with the users  
of my app. I am using Chad F.'s “Rails Recipes” to get me started,  
and everything works great with the join table “people\_friends”. I add  
friends by doing somebody.friends \<\< somebodyelse. However, with my  
app, there is an approval process so my join table has columns  
person\_id, friend\_id, and approved. How do I control the “approved  
field”? When I add a friend, I want to set approved to zero, when they  
are approved, I want to set it to one. I played around with the arrays  
in console, but all that happened was approved was set to null. If I  
can’t figure out another way, I am just going to use sql, but I would  
really like to figure out a better way.

## Thanks!

Randy S.  
removed\_email\_address@domain.invalid  
267.334.6833

---

<div class="post-metadata">

**Author:** ![randy\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f9ae1b/32.png) [@randy\_s](https://www.ruby-forum.com/u/randy_s)\
**Post date:** [February 21, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/self-referencial-habtm-relationship/71436/2 "2006-02-21T18:44:58Z")

</div>

Randy S. \<x [altorg.com](http://altorg.com)\> writes:

> are approved, I want to set it to one. I played around with the arrays  
> in console, but all that happened was approved was set to null. If I  
> can’t figure out another way, I am just going to use sql, but I would  
> really like to figure out a better way.
> 
> ## Thanks!
> 
> Randy S.  
> x [altorg.com](http://altorg.com)  
> 267.334.6833

In the “Agile Web D. with Rails” book, the example is given on  
page 232:

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :articles  
def read\_article(article)  
articles.push\_with\_attributes(article, :read\_at =\> Time.now)  
end  
end

The call to push\_with\_attributes( ) does all the same work of linking  
the two  
models that the \<\< method does, but it also adds the given values to the  
join table row that it creates every time someone reads an article.

---

<div class="post-metadata">

**Author:** ![randy\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f9ae1b/32.png) [@randy\_s](https://www.ruby-forum.com/u/randy_s)\
**Post date:** [February 21, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/self-referencial-habtm-relationship/71436/3 "2006-02-21T18:48:00Z")

</div>

Thanks! I’ll have to give it a try later today.  
Randy

On 2/21/06, Jason C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > person\_id, friend\_id, and approved. How do I control the "approved  
> > 267.334.6833
> 
> The call to push\_with\_attributes( ) does all the same work of linking the two  
> models that the \<\< method does, but it also adds the given values to the  
> join table row that it creates every time someone reads an article.
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Randy S.  
removed\_email\_address@domain.invalid  
267.334.6833

---

<div class="post-metadata">

**Author:** ![randy\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f9ae1b/32.png) [@randy\_s](https://www.ruby-forum.com/u/randy_s)\
**Post date:** [February 21, 2006, 10:57pm UTC](https://www.ruby-forum.com/t/self-referencial-habtm-relationship/71436/4 "2006-02-21T22:57:44Z")

</div>

On 2/21/06, Randy S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks! I’ll have to give it a try later today.  
> Randy

Randy, you might want to check out the new recipe on Join Models (Many  
to Many Relationships where there are attributes on the relationship).  
Join Models are the new favorable way to do many to many “rich”  
relationships like this. I believe the attributes habtm is on the  
potentially-deprecate list.

Thanks!

–  
Chad F.  
[http://chadfowler.com](http://chadfowler.com)  
[http://pragmaticprogrammer.com/titles/fr\_rr/](http://pragmaticprogrammer.com/titles/fr_rr/) (Rails Recipes - In Beta!)  
[http://pragmaticprogrammer.com/titles/mjwti/](http://pragmaticprogrammer.com/titles/mjwti/) (My Job Went to India,  
and All I Got Was This Lousy Book)  
[http://rubycentral.org](http://rubycentral.org)  
[http://rubygarden.org](http://rubygarden.org)  
[http://rubygems.rubyforge.org](http://rubygems.rubyforge.org) (over one million gems served!)

---

<div class="post-metadata">

**Author:** ![randy\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f9ae1b/32.png) [@randy\_s](https://www.ruby-forum.com/u/randy_s)\
**Post date:** [February 22, 2006, 3:27am UTC](https://www.ruby-forum.com/t/self-referencial-habtm-relationship/71436/5 "2006-02-22T03:27:29Z")

</div>

Hi Chad, don’t know if you got my email but thanks again for the  
mention.

I noticed you used most of the code that I posted here:

> **[SitePoint Forums | Web Development & Design Community](https://www.sitepoint.com/community/)**
>
> A community for web designers and developers to discuss everything from HTML, CSS, JavaScript, PHP, to Photoshop, SEO and more.

I was just wondering why you chose to leave out the conditional on the  
delete callback:

def remove\_reverse\_association(associated\_user)  
associated\_user.known\_users.delete(self) if  
associated\_user.known\_users.include?(self)  
end

But in the beta book:

def remove\_reverse\_association(associated\_user)  
associated\_user.known\_users.delete(self)  
end

Did you miss it something or did I get it wrong and its simply not  
needed?

On 2/22/06, Randy S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > On 2/21/06, Randy S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> > Thanks!
> > 
> > * * *
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Cheers,  
Luke R.

> **[Luke Redpath - Freelance Ruby, Rails and iOS app developer / London / Essex / UK](https://lukeredpath.co.uk/)**
>
> Experienced freelance and contract developer offering Ruby, Rails and iOS app development services for iPhone, iPad and other iOS platforms. Based in Colchester, Essex, London and remote.

---

<div class="post-metadata">

**Author:** ![randy\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f9ae1b/32.png) [@randy\_s](https://www.ruby-forum.com/u/randy_s)\
**Post date:** [February 23, 2006, 6:34am UTC](https://www.ruby-forum.com/t/self-referencial-habtm-relationship/71436/6 "2006-02-23T06:34:04Z")

</div>

On 2/21/06, Luke R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Chad, don’t know if you got my email but thanks again for the mention.

Oh I sure didn’t! Sorry about that! And thanks to you!

> end
> 
> But in the beta book:
> 
> def remove\_reverse\_association(associated\_user)  
> associated\_user.known\_users.delete(self)  
> end
> 
> Did you miss it something or did I get it wrong and its simply not needed?

Nothing wrong necessarily, but if you delete something that doesn’t  
already exist, it’s basically a no-op so the conditional isn’t  
strictly necessary. Mostly I just try to keep the recipes as simple  
as possible, so I remove whatever code I can if it’s not completely  
central to teaching the concept.

Thanks!

–  
Chad F.  
[http://chadfowler.com](http://chadfowler.com)  
[http://pragmaticprogrammer.com/titles/fr\_rr/](http://pragmaticprogrammer.com/titles/fr_rr/) (Rails Recipes - In Beta!)  
[http://pragmaticprogrammer.com/titles/mjwti/](http://pragmaticprogrammer.com/titles/mjwti/) (My Job Went to India,  
and All I Got Was This Lousy Book)  
[http://rubycentral.org](http://rubycentral.org)  
[http://rubygarden.org](http://rubygarden.org)  
[http://rubygems.rubyforge.org](http://rubygems.rubyforge.org) (over one million gems served!)

---

<div class="post-metadata">

**Author:** ![randy\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f9ae1b/32.png) [@randy\_s](https://www.ruby-forum.com/u/randy_s)\
**Post date:** [February 22, 2006, 1:21am UTC](https://www.ruby-forum.com/t/self-referencial-habtm-relationship/71436/7 "2006-02-22T01:21:39Z")

</div>

Chad,

As soon as Dave T. sent an email out that a new beta book of Rails  
Recipes was out, I went and checked it out and was happy to see that  
“recipe”, if fit what I needed exactly! I’m enjoying the book. Good  
job so far!

Randy

On 2/21/06, Chad F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [http://rubygems.rubyforge.org](http://rubygems.rubyforge.org) (over one million gems served!)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Randy S.  
removed\_email\_address@domain.invalid  
267.334.6833

---

<div class="post-metadata">

**Author:** ![randy\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f9ae1b/32.png) [@randy\_s](https://www.ruby-forum.com/u/randy_s)\
**Post date:** [February 24, 2006, 4:13am UTC](https://www.ruby-forum.com/t/self-referencial-habtm-relationship/71436/8 "2006-02-24T04:13:49Z")

</div>

Chad,

I’ve been working on making a self-referencial habtm using a join  
model. I got each working independently, but I can’t get the two  
recipes to work together. here are my models:

class User \< ActiveRecord::Base  
has\_many :friends  
has\_and\_belongs\_to\_many :acquaintances, :through =\> :friends,  
:class\_name =\> “User”,  
:join\_table =\> “friends”,  
:association\_foreign\_key =\> “acquaintance\_id”,  
:foreign\_key =\> “user\_id”  
end

class Friend \< ActiveRecord::Base  
belongs\_to :users  
belongs\_to :acquaintances  
end

I tried it with and without an id column in the friends table. Any  
help would be greatly appreciated!

Thanks!  
Randy

On 2/21/06, Chad F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [http://rubygems.rubyforge.org](http://rubygems.rubyforge.org) (over one million gems served!)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Randy S.  
removed\_email\_address@domain.invalid  
267.334.6833
