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
). 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.