# Is it real in RoR?

**URL:** <https://www.ruby-forum.com/t/is-it-real-in-ror/153548>\
**Category:** Rails\
**Created:** [November 18, 2008, 5:05pm UTC](https://www.ruby-forum.com/t/is-it-real-in-ror/153548 "2008-11-18T17:05:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@alexey](https://www.ruby-forum.com/u/alexey)\
**Post date:** [November 18, 2008, 5:05pm UTC](https://www.ruby-forum.com/t/is-it-real-in-ror/153548/1 "2008-11-18T17:05:05Z")

</div>

I’ve a table dic: id, type\_id, info

I’d like to store any information inside, it can be, for instance,  
categories with type\_id = 1 and classifications with type\_id = 2

I’d like to make 2 different models such as

class Category \< ActiveRecord::Base  
set\_table\_name “dic”  
end

class ólassification \< ActiveRecord::Base  
set\_table\_name “dic”  
end

and add conditions to it: type\_id = 1 to first, type\_id = 2 to  
second.

In this way i can use Category.find(:all) and don’t care about  
classification - they won’t be found.  
On the other hand, in ólassification.find(:all) categories won’t be  
found.

Can I add it with RoR?

---

<div class="post-metadata">

**Author:** ![alexey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@alexey](https://www.ruby-forum.com/u/alexey)\
**Post date:** [November 18, 2008, 6:01pm UTC](https://www.ruby-forum.com/t/is-it-real-in-ror/153548/2 "2008-11-18T18:01:33Z")

</div>

On Nov 18, 8:00šam, Alexey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> found.
> 
> Can I add it with RoR?

I’d suggest you use named scopes instead, allowing you to stick with  
Rails conventions and benefits.

class Dic \< ActiceRecord::Base  
set\_table\_name ‘dic’

named\_scope :categories, :conditions =\> { :type\_id =\> 1 }  
named\_scope :classifications, :conditions =\> { :type\_id =\> 2 }

end

# Example usage

Dic.categories.all # =\> returns all categories  
Dic.classifications.all # =\> returns all classifications

Jeff

[purpleworkshops.com](http://purpleworkshops.com)

---

<div class="post-metadata">

**Author:** ![alexey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@alexey](https://www.ruby-forum.com/u/alexey)\
**Post date:** [November 20, 2008, 8:59am UTC](https://www.ruby-forum.com/t/is-it-real-in-ror/153548/3 "2008-11-20T08:59:52Z")

</div>

Hello–

On Nov 18, 2008, at 6:00 AM, Alexey wrote:

> end  
> On the other hand, in Ð¡lassification.find(:all) categories won’t be  
> found.
> 
> Can I add it with RoR?

Apologies if this has already been answered.

It looks like you want single table inheritance  
([http://wiki.rubyonrails.org/rails/pages/singletableinheritance](http://wiki.rubyonrails.org/rails/pages/singletableinheritance)  
). So, if I understand your question correctly, you would have a  
column “type” (not “type\_id”) in your table and write this code:

class Dic \< ActiveRecord::Base  
end

class Category \< Dic  
end

class Classification \< Dic  
end

# …

cat = Category.create(:some =\> ‘value’, :that =\> ‘makes\_sense’)

cls = Classification.create(:some =\> ‘other\_value’, :that =\>  
‘makes\_different\_sense’)

Category.find(:all)  
=\> [#\<Category id: 1, :some: “value”, that: “makes\_sense”, type:  
“Category”\>]

Classification.find(:all)  
=\> [#\< Classification id: 2, :some: “other\_value”, that:  
“makes\_different\_sense”, type: Classification"\>]

Dic.find(:all)  
=\> [#\<Category id: 1, :some: “value”, that: “makes\_sense”, type:  
“Category”\>, #\< Classification id: 2, :some: “other\_value”, that:  
“makes\_different\_sense”, type: Classification"\>]

Because these are different classes they can have different behaviors  
over and above what is provided in the base class. So, not only does  
this select rows cleanly, it also allows you to model the behavior  
according to the type of the data.
