# Has\_many/belongs\_to and DRY

**URL:** <https://www.ruby-forum.com/t/has-many-belongs-to-and-dry/67020>\
**Category:** Rails\
**Created:** [August 30, 2006, 11:38pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-and-dry/67020 "2006-08-30T23:38:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceburgh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c4cdca/32.png) [@iceburgh](https://www.ruby-forum.com/u/iceburgh)\
**Post date:** [August 30, 2006, 11:38pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-and-dry/67020/1 "2006-08-30T23:38:20Z")

</div>

Can someone tell me whether the fact that ActiveRecord  
requires both has\_many and belongs\_to

- isn’t really a violation of DRY

- is a necessary violation of DRY

- is an unnecessary violation of DRY, which (will/won’t)  
go away in time, for the following reason…

That is, does using both statements tell ActiveRecord  
something that it couldn’t figure out from either one?

## -r

[http://www.cfcl.com/rdm](http://www.cfcl.com/rdm) Rich M.  
[http://www.cfcl.com/rdm/resume](http://www.cfcl.com/rdm/resume) removed\_email\_address@domain.invalid  
[http://www.cfcl.com/rdm/weblog](http://www.cfcl.com/rdm/weblog) +1 650-873-7841

Technical editing and writing, programming, and web development

---

<div class="post-metadata">

**Author:** ![iceburgh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c4cdca/32.png) [@iceburgh](https://www.ruby-forum.com/u/iceburgh)\
**Post date:** [August 31, 2006, 12:44am UTC](https://www.ruby-forum.com/t/has-many-belongs-to-and-dry/67020/2 "2006-08-31T00:44:29Z")

</div>

On 30 Aug 2006, at 22:28, Rich M. wrote:

> That is, does using both statements tell ActiveRecord  
> something that it couldn’t figure out from either one?

belongs\_to is a to-one relation, has\_many is a to-many relation, so  
they are obviously different. If you meant belongs\_to and has\_one,  
then the difference is the location of the foreign key - the model  
with belongs\_to will contain a foreign key for the relation, the  
model with has\_one won’t.

Paul

---

<div class="post-metadata">

**Author:** ![iceburgh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c4cdca/32.png) [@iceburgh](https://www.ruby-forum.com/u/iceburgh)\
**Post date:** [August 31, 2006, 1:04am UTC](https://www.ruby-forum.com/t/has-many-belongs-to-and-dry/67020/3 "2006-08-31T01:04:32Z")

</div>

At 11:30 PM +0100 8/30/06, Paul L. wrote:

> belongs\_to is a to-one relation, has\_many is a to-many relation, so  
> they are obviously different.

Sorry, wasn’t being careful.

> If you meant belongs\_to and has\_one, then the difference  
> is the location of the foreign key - the model with  
> belongs\_to will contain a foreign key for the relation,  
> the model with has\_one won’t.

OK, I understand that, but why (aside documentation and/or  
implementation details) does the relationship have to be  
expressed in both models? That is, does “belongs\_to B” in  
A imply “has\_one A” in B?

## -r

[http://www.cfcl.com/rdm](http://www.cfcl.com/rdm) Rich M.  
[http://www.cfcl.com/rdm/resume](http://www.cfcl.com/rdm/resume) removed\_email\_address@domain.invalid  
[http://www.cfcl.com/rdm/weblog](http://www.cfcl.com/rdm/weblog) +1 650-873-7841

Technical editing and writing, programming, and web development

---

<div class="post-metadata">

**Author:** ![iceburgh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c4cdca/32.png) [@iceburgh](https://www.ruby-forum.com/u/iceburgh)\
**Post date:** [August 31, 2006, 1:26am UTC](https://www.ruby-forum.com/t/has-many-belongs-to-and-dry/67020/4 "2006-08-31T01:26:35Z")

</div>

On 30 Aug 2006, at 23:53, Rich M. wrote:

> At 11:30 PM +0100 8/30/06, Paul L. wrote:
> 
> > If you meant belongs\_to and has\_one, then the difference  
> > is the location of the foreign key - the model with  
> > belongs\_to will contain a foreign key for the relation,  
> > the model with has\_one won’t.
> 
> OK, I understand that, but why (aside documentation and/or  
> implementation details) does the relationship have to be  
> expressed in both models? That is, does “belongs\_to B” in  
> A imply “has\_one A” in B?

No, and you might choose to model a one-way relationship for  
performance (or other) reasons. This concept isn’t application to a  
relational database, but is very significant to an OO language  
implementation like ActiveRecord. The example I usually give is of a  
customer database with a country table - often most customers will be  
in one country, with many thousands. It makes sense to model the  
relation from customer to country, but the inverse relationship has  
little value, slows down new customer insertions, and can be faked  
out with a find, anyway, if you really need it.

So, yes, I find the separation necessary.

Paul

---

<div class="post-metadata">

**Author:** ![iceburgh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c4cdca/32.png) [@iceburgh](https://www.ruby-forum.com/u/iceburgh)\
**Post date:** [September 1, 2006, 12:05pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-and-dry/67020/5 "2006-09-01T12:05:45Z")

</div>

Rich M. wrote:

> OK, I understand that, but why (aside documentation and/or  
> implementation details) does the relationship have to be  
> expressed in both models? That is, does “belongs\_to B” in  
> A imply “has\_one A” in B?

Its all to do with implementation details (which I know you said “aside  
from implementation details”), not of the associations, but of dynamic  
model loading.

The model classes aren’t loaded into memory when your rails app is  
launched, instead they are loaded when you first ask for them (Dr Nic’s  
Magic Models extends this idea by creating the model class even if it  
doesn’t exist in your application).

So, if you had Person and Membership classes, where Person.has\_many  
:memberships; and Membership.belongs\_to :person; it is theoretically  
possible for the Person.has\_many class method to invoke  
Membership.belongs\_to :person automatically for you.

But, what if you access the Membership class first? Due to the dynamic  
loading, if the Person class hadn’t been loaded yet, it wouldn’t have  
generated the has\_many and belongs\_to associations for us. Our  
Membership class would be ignorant of its relationships with other  
classes.

Inner Rails workings are fun, eh? 🙂

Cheers  
Nic

---

<div class="post-metadata">

**Author:** ![iceburgh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c4cdca/32.png) [@iceburgh](https://www.ruby-forum.com/u/iceburgh)\
**Post date:** [September 1, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-and-dry/67020/6 "2006-09-01T18:26:54Z")

</div>

> OK, I understand that, but why (aside documentation and/or  
> implementation details) does the relationship have to be  
> expressed in both models? That is, does “belongs\_to B” in  
> A imply “has\_one A” in B?

Just down to implementing the the methods that are added to each class  
because of their end of the relationship. I find it elegant to know  
within the model I am editing what it’s end of the relationship is and  
with whom so it is just that bit kinder to the developer.

---

<div class="post-metadata">

**Author:** ![iceburgh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c4cdca/32.png) [@iceburgh](https://www.ruby-forum.com/u/iceburgh)\
**Post date:** [August 31, 2006, 1:14am UTC](https://www.ruby-forum.com/t/has-many-belongs-to-and-dry/67020/7 "2006-08-31T01:14:34Z")

</div>

> > That is, does using both statements tell ActiveRecord  
> > something that it couldn’t figure out from either one?
> 
> belongs\_to is a to-one relation, has\_many is a to-many relation, so  
> they are obviously different. If you meant belongs\_to and has\_one,  
> then the difference is the location of the foreign key - the model  
> with belongs\_to will contain a foreign key for the relation, the  
> model with has\_one won’t.

i think what he means is that if a model A has a has\_many B, then model  
B will always belong\_to model A, and this doesn’t need to be redefined.  
On the other hand if model B belongs\_to model A then model A doesn’t  
necessarily has\_many B

Dr Nic’s Magic Models would answer your question, then you don’t need to  
define any of it.
