# Modeling help needed

**URL:** <https://www.ruby-forum.com/t/modeling-help-needed/140700>\
**Category:** Rails\
**Created:** [June 17, 2008, 5:55pm UTC](https://www.ruby-forum.com/t/modeling-help-needed/140700 "2008-06-17T17:55:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jc71229](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jc71229](https://www.ruby-forum.com/u/jc71229)\
**Post date:** [June 17, 2008, 5:55pm UTC](https://www.ruby-forum.com/t/modeling-help-needed/140700/1 "2008-06-17T17:55:12Z")

</div>

Hi,

I have a modeling problem I can’t figure out yet.

Say I have an club of members and every member can have  
favorite other members.

class Member \< ActiveRecord::Base  
has\_many :favorites  
end

class Favorite \< ActiveRecord::Base  
belongs\_to :member, :foreign\_key =\> :member\_id  
belongs\_to :member, :foreign\_key =\> :favorite\_id  
end

@tim = Member.create(:name =\> “Tim”)  
@foo = Member.create(:name =\> “Foo”)

@joe = Member.new(:name =\> “Joe”)  
@joe.favorites \<\< Favorite.create(@tim)  
@joe.favorites \<\< Favorite.create(@foo)  
@joe.save

I guess that’s not right. I am obviously missing something.  
Any help appreciated

---

<div class="post-metadata">

**Author:** ![jc71229](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jc71229](https://www.ruby-forum.com/u/jc71229)\
**Post date:** [June 17, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/modeling-help-needed/140700/2 "2008-06-17T18:02:42Z")

</div>

class Member…  
has\_many :favorites  
has\_many :favorite\_members, :class\_name =\> “Member”, :through  
=\> :favorites  
end

class Favorite …  
belongs\_to :member  
belongs\_to :favorite\_member, :class\_name =\> “Member”, :foreign\_key  
=\> :favorite\_id  
end

…  
…  
@joe.favorite\_members \<\< @foo  
@joe.save

That’s how I do it. Though I use friends and friendship, but it’s  
basically the same thing.

---

<div class="post-metadata">

**Author:** ![jc71229](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jc71229](https://www.ruby-forum.com/u/jc71229)\
**Post date:** [June 17, 2008, 6:08pm UTC](https://www.ruby-forum.com/t/modeling-help-needed/140700/3 "2008-06-17T18:08:45Z")

</div>

> …  
> …  
> @joe.favorite\_members \<\< @foo  
> @joe.save
> 
> That’s how I do it. Though I use friends and friendship, but it’s  
> basically the same thing.

Thanks that was fast! I’ll give it a shot.

---

<div class="post-metadata">

**Author:** ![jc71229](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jc71229](https://www.ruby-forum.com/u/jc71229)\
**Post date:** [June 18, 2008, 1:46pm UTC](https://www.ruby-forum.com/t/modeling-help-needed/140700/4 "2008-06-18T13:46:30Z")

</div>

Thanks to silkcom, that works pretty well. However I have to maintain an  
additional comment field in my favorite (join) model.

How do I access that comment (read or update) in favorite?

With this I can iterate over all favorites, however …

@joe.favorite\_members.each do |f|  
end

… with f I’ll access Members, but I would like to access each comment  
in Favorites.

Thanks again
