# Same has\_many and has\_one?

**URL:** <https://www.ruby-forum.com/t/same-has-many-and-has-one/172202>\
**Category:** Rails\
**Created:** [July 16, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/same-has-many-and-has-one/172202 "2009-07-16T17:16:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [July 16, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/same-has-many-and-has-one/172202/1 "2009-07-16T17:16:22Z")

</div>

I think I’ve just thoroughly confused myself at this point. I have a  
class that has a has\_many and has\_one relationship that use the same  
object.

Here is an example:

class Person \< ActiveRecord::Base  
has\_many :hobbies  
has\_one :favorite\_hobby, :class\_name =\> “Hobby”  
end

class Hobby \< ActiveRecord::Base  
belongs\_to :person  
belongs\_to :person, :foreign\_key =\> “favorite\_hobby\_id”  
end

Of course, this doesn’t work. Any ideas what I need to change to get  
this to work properly?

Thanks.

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [July 16, 2009, 9:47pm UTC](https://www.ruby-forum.com/t/same-has-many-and-has-one/172202/2 "2009-07-16T21:47:34Z")

</div>

The B. wrote:

> I think I’ve just thoroughly confused myself at this point. I have a  
> class that has a has\_many and has\_one relationship that use the same  
> object.
> 
> Here is an example:
> 
> class Person \< ActiveRecord::Base  
> has\_many :hobbies  
> has\_one :favorite\_hobby, :class\_name =\> “Hobby”  
> end
> 
> class Hobby \< ActiveRecord::Base  
> belongs\_to :person  
> belongs\_to :person, :foreign\_key =\> “favorite\_hobby\_id”  
> end
> 
> Of course, this doesn’t work. Any ideas what I need to change to get  
> this to work properly?
> 
> Thanks.

I think you should be using habtm for the hobbies (two people can enjoy  
the same hobby, right?) and an additional belongs\_to for the favorite.  
This may seem counterintuitive, but remember that belongs\_to is used on  
the table with the foreign key field (which in this case is  
people.favorite\_hobby\_id). So:

class Person  
habtm :hobbies  
belongs\_to :favorite\_hobby, :class\_name =\> ‘Hobby’  
end

class Hobby  
habtm :people

# reverse association probably unnecessary for favorite\_hobby

end

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [July 16, 2009, 9:57pm UTC](https://www.ruby-forum.com/t/same-has-many-and-has-one/172202/3 "2009-07-16T21:57:45Z")

</div>

Hrm - maybe I should just quote my actual code rather than the example.  
Actually, technically, no - the “hobbies” are NOT shared between people.  
So two people can’t share the same hobby.

Each Person has their own list of hobbies, and one of those is their  
“favorite” and I dont want to have some “favorite” boolean. My actual  
code has nothing to do with people and hobbies, this was just a (poor)  
example.

Thanks for the help!

Marnen Laibow-Koser wrote:

> The B. wrote:
> 
> > I think I’ve just thoroughly confused myself at this point. I have a  
> > class that has a has\_many and has\_one relationship that use the same  
> > object.
> > 
> > Here is an example:
> > 
> > class Person \< ActiveRecord::Base  
> > has\_many :hobbies  
> > has\_one :favorite\_hobby, :class\_name =\> “Hobby”  
> > end
> > 
> > class Hobby \< ActiveRecord::Base  
> > belongs\_to :person  
> > belongs\_to :person, :foreign\_key =\> “favorite\_hobby\_id”  
> > end
> > 
> > Of course, this doesn’t work. Any ideas what I need to change to get  
> > this to work properly?
> > 
> > Thanks.
> 
> I think you should be using habtm for the hobbies (two people can enjoy  
> the same hobby, right?) and an additional belongs\_to for the favorite.  
> This may seem counterintuitive, but remember that belongs\_to is used on  
> the table with the foreign key field (which in this case is  
> people.favorite\_hobby\_id). So:
> 
> class Person  
> habtm :hobbies  
> belongs\_to :favorite\_hobby, :class\_name =\> ‘Hobby’  
> end
> 
> class Hobby  
> habtm :people
> 
> # reverse association probably unnecessary for favorite\_hobby
> 
> end
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [July 16, 2009, 11:53pm UTC](https://www.ruby-forum.com/t/same-has-many-and-has-one/172202/4 "2009-07-16T23:53:15Z")

</div>

Rick wrote:  
[…]

> I’m new so I don’t have the full answer, but without the favorite  
> boolean,  
> behind the scenes I think a table of “HobbyFavorite” would be needed  
> that  
> has person\_id and hobby\_id, unless I’m missing something.

Since there would be a 1:1 association between Person and HobbyFavorite,  
that would actually be functionally equivalent to just having a  
hobby\_favorite\_id in Person.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [July 17, 2009, 3:03pm UTC](https://www.ruby-forum.com/t/same-has-many-and-has-one/172202/5 "2009-07-17T15:03:25Z")

</div>

Marnen Laibow-Koser wrote:

> Rick wrote:  
> […]
> 
> > I’m new so I don’t have the full answer, but without the favorite  
> > boolean,  
> > behind the scenes I think a table of “HobbyFavorite” would be needed  
> > that  
> > has person\_id and hobby\_id, unless I’m missing something.
> 
> Since there would be a 1:1 association between Person and HobbyFavorite,  
> that would actually be functionally equivalent to just having a  
> hobby\_favorite\_id in Person.

That is exactly how the database looks. Instead of people and hobbies,  
my code actually uses Users and Characters. The People/Hobby was just  
another example. Each user can have any number of characters, but only  
one character is marked as the “selected character”.

The users table has a selected\_character\_id, and the characters table  
has a user\_id.

Thanks.

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [July 17, 2009, 3:52pm UTC](https://www.ruby-forum.com/t/same-has-many-and-has-one/172202/6 "2009-07-17T15:52:23Z")

</div>

The B. wrote:  
[…]

> That is exactly how the database looks. Instead of people and hobbies,  
> my code actually uses Users and Characters. The People/Hobby was just  
> another example. Each user can have any number of characters, but only  
> one character is marked as the “selected character”.
> 
> The users table has a selected\_character\_id, and the characters table  
> has a user\_id.
> 
> Thanks.

OK…then how about

class User  
has\_many :characters  
belongs\_to :selected\_character, :class\_name =\> ‘Character’  
end

class Character  
belongs\_to :user  
end

?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [July 16, 2009, 11:03pm UTC](https://www.ruby-forum.com/t/same-has-many-and-has-one/172202/7 "2009-07-16T23:03:25Z")

</div>

On Thu, Jul 16, 2009 at 3:57 PM, The B.  
\<removed\_email\_address@domain.invalid

> wrote:

> Hrm - maybe I should just quote my actual code rather than the example.  
> Actually, technically, no - the “hobbies” are NOT shared between people.  
> So two people can’t share the same hobby.
> 
> Each Person has their own list of hobbies, and one of those is their  
> “favorite” and I dont want to have some “favorite” boolean. My actual  
> code has nothing to do with people and hobbies, this was just a (poor)  
> example.

I’m new so I don’t have the full answer, but without the favorite  
boolean,  
behind the scenes I think a table of “HobbyFavorite” would be needed  
that  
has person\_id and hobby\_id, unless I’m missing something.

So it sounds like you need two “has\_one” relationships defined?

both Person and Hobby would have:  
has\_one: favorite\_hobby

You then could have a FavoriteHobby model object with  
belongs\_to: person  
belongs\_to: hobby

I’m not 100% this would work or is a good idea, but nobody else has  
suggested anything yet, so I’m trying to learn in the process. Take any  
of  
the above with a HUGE chunk of salt:)

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [July 17, 2009, 4:37pm UTC](https://www.ruby-forum.com/t/same-has-many-and-has-one/172202/8 "2009-07-17T16:37:38Z")

</div>

> OK…then how about
> 
> class User  
> has\_many :characters  
> belongs\_to :selected\_character, :class\_name =\> ‘Character’  
> end
> 
> class Character  
> belongs\_to :user  
> end

That did it! I have no idea why my brain tends to reverse the belongs  
and has relationships sometimes. Thanks a lot for your help.

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [July 17, 2009, 4:41pm UTC](https://www.ruby-forum.com/t/same-has-many-and-has-one/172202/9 "2009-07-17T16:41:30Z")

</div>

The B. wrote:  
[…]

> That did it! I have no idea why my brain tends to reverse the belongs  
> and has relationships sometimes.

Well, in this case, the selected\_character relationship is backwards  
from what it “should” be. I wouldn’t have figured that out immediately  
either. 🙂

> Thanks a lot for your help.

You’re welcome.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
