# Is\_type\_of and is\_a\_category\_for vs: belongs\_to and has\_many

**URL:** <https://www.ruby-forum.com/t/is-type-of-and-is-a-category-for-vs-belongs-to-and-has-many/132173>\
**Category:** Rails\
**Created:** [March 17, 2008, 4:58pm UTC](https://www.ruby-forum.com/t/is-type-of-and-is-a-category-for-vs-belongs-to-and-has-many/132173 "2008-03-17T16:58:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [March 17, 2008, 4:58pm UTC](https://www.ruby-forum.com/t/is-type-of-and-is-a-category-for-vs-belongs-to-and-has-many/132173/1 "2008-03-17T16:58:00Z")

</div>

Hello experts,  
I wonder if I’m the only one who is running into this conceptual  
stumbling block…

Suppose I have a database of documents, and I want to categorize the  
documents. I can model this as:

class document\_category \< ActiveRecord::Base  
has\_many :documents  
end

class document \< ActiveRecord::Base  
belongs\_to :document\_category  
end

but, semantically, I would rather write:

class document \< ActiveRecord::Base  
is\_type\_of :document\_category  
end

class document\_category \< ActiveRecord::Base  
is\_a\_category\_for :documents  
end

(Or use some other sort of verbiage).  
The focus of my database is on the the documents, not the categories.  
The user entry fields are all going to be document-centric, (with,  
probably a drop-down box selecting the category for a document).

This is inverted from the classic Rails tutorials where a post might  
have many comments, or a supplier might have many widgets.

I am curious what folks do in this situation… Have you just learned  
(as I will) to live with the underlying meaning of “has\_many” and  
“belongs\_to”? Or do you define your own relationship mappings  
according to your problem domain. (If so, how would one do that?)

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [March 17, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/is-type-of-and-is-a-category-for-vs-belongs-to-and-has-many/132173/2 "2008-03-17T20:35:27Z")

</div>

Patrick D. wrote:

> but, semantically, I would rather write:
> 
> class document \< ActiveRecord::Base  
> is\_type\_of :document\_category  
> end
> 
> class document\_category \< ActiveRecord::Base  
> is\_a\_category\_for :documents  
> end

At the end of config/environment.rb, if you put:

class \<\< ActiveRecord::Base  
alias\_method :is\_type\_of, :belongs\_to  
alias\_method :is\_a\_category\_for, :has\_many  
end

then you can use these instead.

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [March 17, 2008, 9:09pm UTC](https://www.ruby-forum.com/t/is-type-of-and-is-a-category-for-vs-belongs-to-and-has-many/132173/3 "2008-03-17T21:09:17Z")

</div>

On Mon, Mar 17, 2008 at 3:35 PM, Mark B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> At the end of config/environment.rb, if you put:
> 
> class \<\< ActiveRecord::Base  
> alias\_method :is\_type\_of, :belongs\_to  
> alias\_method :is\_a\_category\_for, :has\_many  
> end
> 
> then you can use these instead.  
> Thanks. That’s a handy trick to know. I’ll use that.

From a coding style perspective, is this what people generally do? Or  
does everybody accept and understand the semantic meanings of  
belongs\_to and has\_many?

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [March 17, 2008, 9:46pm UTC](https://www.ruby-forum.com/t/is-type-of-and-is-a-category-for-vs-belongs-to-and-has-many/132173/4 "2008-03-17T21:46:08Z")

</div>

Patrick D. wrote:

> From a coding style perspective, is this what people generally do? Or  
> does everybody accept and understand the semantic meanings of  
> belongs\_to and has\_many?

Personally, I always just use has\_many/belongs\_to as that is the  
relationship between the models.

If you use something non-standard, then someone else coming to your code  
would have to look for the definition in documentation or the source  
somewhere, no matter how clear the names, to be certain in themselves  
what is meant. That doesn’t mean you shouldn’t do it. Rails is  
opinionated, but not restrictive.
